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