|
| 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. The implementation is a stub implementation that does not support connection pooling or keep-alive. |
| 13 | + |
| 14 | +```js |
| 15 | +import { Agent } from 'node:http'; |
| 16 | +import { strictEqual } from 'node:assert/strict'; |
| 17 | + |
| 18 | +const agent = new Agent(); |
| 19 | +strictEqual(agent.protocol, 'http:'); |
| 20 | +``` |
| 21 | + |
| 22 | +## get |
| 23 | + |
| 24 | +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. |
| 25 | + |
| 26 | +```js |
| 27 | +import { get } from 'node:http'; |
| 28 | +import { strictEqual } from 'node:assert/strict'; |
| 29 | + |
| 30 | +get('http://docs.cloudflare.com/robots.txt', (res) => { |
| 31 | + // http://docs.cloudflare.com redirects to https url. |
| 32 | + strictEqual(res.statusCode, 301); |
| 33 | +}); |
| 34 | +``` |
| 35 | + |
| 36 | +## request |
| 37 | + |
| 38 | +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. |
| 39 | + |
| 40 | +```js |
| 41 | +import { request } from 'node:http'; |
| 42 | +import { strictEqual } from 'node:assert/strict'; |
| 43 | + |
| 44 | +request({ |
| 45 | + method: 'GET', |
| 46 | + protocol: 'http:', |
| 47 | + hostname: 'docs.cloudflare.com', |
| 48 | + path: '/' |
| 49 | +}, (res) => { |
| 50 | + // http://docs.cloudflare.com redirects to https url. |
| 51 | + strictEqual(res.statusCode, 301); |
| 52 | +}); |
| 53 | + |
| 54 | +request(new URL('http://docs.cloudflare.com'),{ |
| 55 | + method: 'GET', |
| 56 | +}, (res) => { |
| 57 | + // http://docs.cloudflare.com redirects to https url. |
| 58 | + strictEqual(res.statusCode, 301); |
| 59 | +}); |
| 60 | +``` |
| 61 | + |
| 62 | +The following options passed to the `request` method are not supported: |
| 63 | +- `maxHeaderSize` |
| 64 | +- `insecureHTTPParser` |
| 65 | +- `createConnection` |
| 66 | +- `lookup` |
| 67 | +- `socketPath` |
| 68 | + |
| 69 | +## WebSocket |
| 70 | + |
| 71 | +The `WebSocket` class provides a convenient way to establish a WebSocket connection to a server. It simplifies the process of creating and managing WebSocket connections. |
| 72 | + |
| 73 | +This API is exactly modeled after Node.js' [`WebSocket`](https://nodejs.org/docs/latest/api/globals.html#class-websocket) class. |
| 74 | + |
| 75 | +```js |
| 76 | +import { WebSocket } from 'node:http'; |
| 77 | + |
| 78 | +const ws = new WebSocket('ws://example.com/ws'); |
| 79 | + |
| 80 | +ws.on('open', () => { |
| 81 | + console.log('WebSocket connection opened'); |
| 82 | +}); |
| 83 | + |
| 84 | +ws.on('message', (data) => { |
| 85 | + console.log('Received message:', data); |
| 86 | +}); |
| 87 | + |
| 88 | +ws.on('close', () => { |
| 89 | + console.log('WebSocket connection closed'); |
| 90 | +}); |
| 91 | +``` |
| 92 | + |
| 93 | +## OutgoingMessage |
| 94 | + |
| 95 | +The `OutgoingMessage` 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). |
| 96 | + |
| 97 | +```js |
| 98 | +import { OutgoingMessage } from 'node:http'; |
| 99 | + |
| 100 | +const res = new OutgoingMessage(); |
| 101 | +res.writeHead(200, { 'Content-Type': 'text/plain' }); |
| 102 | +res.write('Hello, World!'); |
| 103 | +res.end(); |
| 104 | +``` |
| 105 | +## IncomingMessage |
| 106 | + |
| 107 | +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. |
| 108 | + |
| 109 | +```js |
| 110 | +import { get, IncomingMessage } from 'node:http'; |
| 111 | +import { ok, strictEqual } from 'node:assert/strict'; |
| 112 | + |
| 113 | +get('http://docs.cloudflare.com', (res) => { |
| 114 | + strictEqual(res.statusCode, 301); |
| 115 | + ok(res instanceof IncomingMessage); |
| 116 | +}); |
| 117 | +``` |
0 commit comments