|
| 1 | +--- |
| 2 | +pcx_content_type: configuration |
| 3 | +title: http |
| 4 | +--- |
| 5 | + |
| 6 | +import { Render } from "~/components" |
| 7 | + |
| 8 | +<Render file="nodejs-compat-howto" /> |
| 9 | + |
| 10 | +## Agent |
| 11 | + |
| 12 | +An implementation of the Node.js [`http.Agent'](https://nodejs.org/docs/latest/api/http.html#class-httpagent) class. |
| 13 | + |
| 14 | +An [Agent](https://nodejs.org/docs/latest/api/http.html#class-httpagent) manages HTTP connection reuse by maintaining request queues per host/port. In the |
| 15 | +workers environment, however, such low-level management of the network connection, ports, |
| 16 | +etc, is not relevant because it is handled by the Cloudflare infrastructure instead. Accordingly, the |
| 17 | +implementation of `Agent` in Workers is a stub implementation that does not support connection |
| 18 | +pooling or keep-alive. |
| 19 | + |
| 20 | +```js |
| 21 | +import { Agent } from 'node:http'; |
| 22 | +import { strictEqual } from 'node:assert'; |
| 23 | + |
| 24 | +const agent = new Agent(); |
| 25 | +strictEqual(agent.protocol, 'http:'); |
| 26 | +``` |
| 27 | + |
| 28 | +## get |
| 29 | + |
| 30 | +An implementation of the Node.js [`http.get`](https://nodejs.org/docs/latest/api/http.html#httpgetoptions-callback) method. |
| 31 | + |
| 32 | +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. |
| 33 | + |
| 34 | +```js |
| 35 | +import { get } from 'node:http'; |
| 36 | +import { strictEqual, ok } from 'node:assert'; |
| 37 | + |
| 38 | +get('http://docs.cloudflare.com/robots.txt', (res) => { |
| 39 | + // requests to http://docs.cloudflare.com get redirected to their https counterpart. |
| 40 | + strictEqual(res.statusCode, 301); |
| 41 | + let data = ''; |
| 42 | + res.setEncoding('utf8'); |
| 43 | + res.on('data', (chunk) => { |
| 44 | + data += chunk; |
| 45 | + }); |
| 46 | + res.on('end', () => { |
| 47 | + ok(data.includes('301 Moved Permanently')); |
| 48 | + }); |
| 49 | +}); |
| 50 | +``` |
| 51 | + |
| 52 | +## request |
| 53 | + |
| 54 | +An implementation of the Node.js [`http.request'](https://nodejs.org/docs/latest/api/http.html#httprequesturl-options-callback) method. |
| 55 | + |
| 56 | +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. |
| 57 | + |
| 58 | +```js |
| 59 | +import { request } from 'node:http'; |
| 60 | +import { strictEqual } from 'node:assert'; |
| 61 | + |
| 62 | +const req = request({ |
| 63 | + method: 'GET', |
| 64 | + protocol: 'http:', |
| 65 | + hostname: 'docs.cloudflare.com', |
| 66 | + path: '/' |
| 67 | +}, (res) => { |
| 68 | + // requests to http://docs.cloudflare.com get redirected to their https counterpart. |
| 69 | + strictEqual(res.statusCode, 301); |
| 70 | + |
| 71 | + let data = ''; |
| 72 | + res.setEncoding('utf8'); |
| 73 | + res.on('data', (chunk) => { |
| 74 | + data += chunk; |
| 75 | + }); |
| 76 | + res.on('end', () => { |
| 77 | + ok(data.includes('301 Moved Permanently')); |
| 78 | + }); |
| 79 | +}); |
| 80 | +req.end(); |
| 81 | +``` |
| 82 | + |
| 83 | +```js |
| 84 | +const req = request(new URL('http://docs.cloudflare.com'),{ |
| 85 | + method: 'GET', |
| 86 | +}, (res) => { |
| 87 | + // requests to http://docs.cloudflare.com get redirected to their https counterpart. |
| 88 | + strictEqual(res.statusCode, 301); |
| 89 | +}); |
| 90 | + |
| 91 | +req.end(); |
| 92 | +``` |
| 93 | + |
| 94 | +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: |
| 95 | +- `maxHeaderSize` |
| 96 | +- `insecureHTTPParser` |
| 97 | +- `createConnection` |
| 98 | +- `lookup` |
| 99 | +- `socketPath` |
| 100 | + |
| 101 | +## OutgoingMessage |
| 102 | + |
| 103 | +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). |
| 104 | + |
| 105 | +```js |
| 106 | +import { OutgoingMessage } from 'node:http'; |
| 107 | + |
| 108 | +const res = new OutgoingMessage(); |
| 109 | +res.writeHead(200, { 'Content-Type': 'text/plain' }); |
| 110 | +res.write('Hello, World!'); |
| 111 | +res.end(); |
| 112 | +``` |
| 113 | +## IncomingMessage |
| 114 | + |
| 115 | +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. |
| 116 | + |
| 117 | +```js |
| 118 | +import { get, IncomingMessage } from 'node:http'; |
| 119 | +import { ok, strictEqual } from 'node:assert'; |
| 120 | + |
| 121 | +get('http://docs.cloudflare.com', (res) => { |
| 122 | + strictEqual(res.statusCode, 301); |
| 123 | + ok(res instanceof IncomingMessage); |
| 124 | +}); |
| 125 | +``` |
0 commit comments