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