-
Notifications
You must be signed in to change notification settings - Fork 10k
Add node:http and node:https modules #23481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
6ddc2a0
990cc94
be5db3c
90cbe94
de7fe07
dfba2b1
bc74bec
5198119
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| --- | ||
| pcx_content_type: configuration | ||
| title: http | ||
| --- | ||
|
|
||
| import { Render } from "~/components" | ||
|
|
||
| <Render file="nodejs-compat-howto" /> | ||
|
|
||
| ## Agent | ||
|
|
||
| An Agent manages HTTP connection reuse by maintaining request queues per host/port. In the | ||
| workers environment, however, such low-level management of the network connection, ports, | ||
| etc, is not relevant because it is handled by the Cloudflare infrastructure instead. Accordingly, the | ||
| implementation of `Agent` in Workers is a stub implementation that does not support connection | ||
| pooling or keep-alive. | ||
|
|
||
| ```js | ||
| import { Agent } from 'node:http'; | ||
| import { strictEqual } from 'node:assert'; | ||
|
|
||
| const agent = new Agent(); | ||
| strictEqual(agent.protocol, 'http:'); | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## get | ||
|
|
||
| The `get` method performs a GET request to the specified URL and invokes the callback with the response. It's a convenience method that simplifies making HTTP GET requests without manually configuring request options. | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```js | ||
| import { get } from 'node:http'; | ||
| import { strictEqual, ok } from 'node:assert'; | ||
|
|
||
| get('http://docs.cloudflare.com/robots.txt', (res) => { | ||
| // http://docs.cloudflare.com redirects to https url. | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| strictEqual(res.statusCode, 301); | ||
| let data = ''; | ||
| res.setEncoding('utf8'); | ||
| res.on('data', (chunk) => { | ||
| data += chunk; | ||
| }); | ||
| res.on('end', () => { | ||
| // Returns the response body as a string. | ||
| console.log(data); | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ok(data.includes('301 Moved Permanently')); | ||
| }); | ||
| }); | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ## request | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had a comment somewhere that appears to have been marked resolved about how these should include links to the relevant node.js docs for these methods. I still think we should include those links.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean the header should be a link to node.js docs? Can you be specific?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jasnell is the current state sufficient? |
||
|
|
||
| The `request` method creates an HTTP request with customizable options like method, headers, and body. It provides full control over the request configuration and returns a [writable stream](https://nodejs.org/docs/latest/api/stream.html#class-streamwritable) for sending request data. | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```js | ||
| import { request } from 'node:http'; | ||
| import { strictEqual } from 'node:assert'; | ||
|
|
||
| const req = request({ | ||
| method: 'GET', | ||
| protocol: 'http:', | ||
| hostname: 'docs.cloudflare.com', | ||
| path: '/' | ||
| }, (res) => { | ||
| // http://docs.cloudflare.com redirects to https url. | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| strictEqual(res.statusCode, 301); | ||
|
|
||
| let data = ''; | ||
| res.setEncoding('utf8'); | ||
| res.on('data', (chunk) => { | ||
| data += chunk; | ||
| }); | ||
| res.on('end', () => { | ||
| // Returns the response body as a string. | ||
| console.log(data); | ||
| ok(data.includes('301 Moved Permanently')); | ||
| }); | ||
| }); | ||
| req.end(); | ||
|
|
||
| const req2 = request(new URL('http://docs.cloudflare.com'),{ | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| method: 'GET', | ||
| }, (res) => { | ||
| // http://docs.cloudflare.com redirects to https url. | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| strictEqual(res.statusCode, 301); | ||
| }); | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| req2.end(); | ||
| ``` | ||
|
|
||
| The following options passed to the `request` method are not supported due to the differences in the Cloudflare Workers and the implementation of the node:http module: | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - `maxHeaderSize` | ||
| - `insecureHTTPParser` | ||
| - `createConnection` | ||
| - `lookup` | ||
| - `socketPath` | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## OutgoingMessage | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| The [`OutgoingMessage`](https://nodejs.org/docs/latest/api/http.html#class-httpoutgoingmessage) class represents an HTTP response that is sent to the client. It provides methods for writing response headers and body, as well as for ending the response. `OutgoingMessage` extends from the [`Writable` stream class](https://nodejs.org/docs/latest/api/stream.html#class-streamwritable). | ||
|
|
||
| ```js | ||
| import { OutgoingMessage } from 'node:http'; | ||
|
|
||
| const res = new OutgoingMessage(); | ||
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
| res.write('Hello, World!'); | ||
| res.end(); | ||
| ``` | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## IncomingMessage | ||
|
|
||
| The `IncomingMessage` class represents an HTTP request that is received from the client. It provides methods for reading request headers and body, as well as for ending the request. `IncomingMessage` extends from the `Readable` stream class. | ||
|
|
||
| ```js | ||
| import { get, IncomingMessage } from 'node:http'; | ||
| import { ok, strictEqual } from 'node:assert'; | ||
|
|
||
| get('http://docs.cloudflare.com', (res) => { | ||
| strictEqual(res.statusCode, 301); | ||
| ok(res instanceof IncomingMessage); | ||
| }); | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| --- | ||
| pcx_content_type: configuration | ||
| title: https | ||
| --- | ||
|
|
||
| import { Render } from "~/components" | ||
|
|
||
| <Render file="nodejs-compat-howto" /> | ||
|
|
||
| ## Agent | ||
|
|
||
| An Agent manages HTTPS connection reuse by maintaining request queues per host/port. In the | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| workers environment, however, such low-level management of the network connection, ports, | ||
| etc, is not relevant because it is handled by the Cloudflare infrastructure instead. Accordingly, the | ||
| implementation of `Agent` in Workers is a stub implementation that does not support connection | ||
| pooling or keep-alive. | ||
|
|
||
| ```js | ||
| import { Agent } from 'node:https'; | ||
| import { strictEqual } from 'node:assert'; | ||
|
|
||
| const agent = new Agent(); | ||
| strictEqual(agent.protocol, 'https:'); | ||
| ``` | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## get | ||
|
|
||
| The [`get`](https://nodejs.org/docs/latest/api/https.html#httpsgetoptions-callback) method performs a GET request to the specified URL and invokes the callback with the response. It's a convenience method that simplifies making HTTPS GET requests without manually configuring request options. | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| `get` methods behaves the same as `node:http` get method. | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```js | ||
| import { get } from 'node:https'; | ||
| import { strictEqual, ok } from 'node:assert'; | ||
|
|
||
| get('https://developers.cloudflare.com/robots.txt', (res) => { | ||
| strictEqual(res.statusCode, 200); | ||
| let data = ''; | ||
| res.setEncoding('utf8'); | ||
| res.on('data', (chunk) => { | ||
| data += chunk; | ||
| }); | ||
| res.on('end', () => { | ||
| // Returns the response body as a string. | ||
| console.log(data); | ||
| ok(data.includes('User-agent')); | ||
| }); | ||
| }); | ||
| ``` | ||
|
|
||
| ## request | ||
|
|
||
| The [`request`](https://nodejs.org/docs/latest/api/https.html#httpsrequesturl-options-callback) method creates an HTTPS request with customizable options like method, headers, and body. It provides full control over the request configuration and returns a [writable stream](https://nodejs.org/docs/latest/api/stream.html#class-streamwritable) for sending request data. | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Request method accepts all options from `http.request` with some differences in default values: | ||
| - `protocol`: default `https:` | ||
| - `port`: default `443` | ||
| - `agent`: default `https.globalAgent` | ||
|
|
||
| The following additional options are not supported: ca, cert, ciphers, clientCertEngine (deprecated), crl, dhparam, ecdhCurve, honorCipherOrder, key, passphrase, pfx, rejectUnauthorized, secureOptions, secureProtocol, servername, sessionIdContext, highWaterMark. | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```js | ||
| import { request } from 'node:https'; | ||
| import { strictEqual, ok } from 'node:assert'; | ||
|
|
||
| const req = request('https://developers.cloudflare.com/robots.txt', { | ||
| method: 'GET', | ||
| }, (res) => { | ||
| strictEqual(res.statusCode, 200); | ||
| let data = ''; | ||
| res.setEncoding('utf8'); | ||
| res.on('data', (chunk) => { | ||
| data += chunk; | ||
| }); | ||
| res.on('end', () => { | ||
| // Returns the response body as a string. | ||
| console.log(data); | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ok(data.includes('User-agent')); | ||
| }); | ||
| }); | ||
| req.end(); | ||
| ``` | ||
Uh oh!
There was an error while loading. Please reload this page.