Skip to content

Commit b304599

Browse files
committed
add node:http and node:https modules
1 parent 5a83835 commit b304599

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
const req = 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+
req.end();
54+
55+
const req2 = request(new URL('http://docs.cloudflare.com'),{
56+
method: 'GET',
57+
}, (res) => {
58+
// http://docs.cloudflare.com redirects to https url.
59+
strictEqual(res.statusCode, 301);
60+
});
61+
62+
req2.end();
63+
```
64+
65+
The following options passed to the `request` method are not supported:
66+
- `maxHeaderSize`
67+
- `insecureHTTPParser`
68+
- `createConnection`
69+
- `lookup`
70+
- `socketPath`
71+
72+
## WebSocket
73+
74+
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.
75+
76+
This API is exactly modeled after Node.js' [`WebSocket`](https://nodejs.org/docs/latest/api/globals.html#class-websocket) class.
77+
78+
```js
79+
import { WebSocket } from 'node:http';
80+
81+
const ws = new WebSocket('ws://example.com/ws');
82+
83+
ws.on('open', () => {
84+
console.log('WebSocket connection opened');
85+
});
86+
87+
ws.on('message', (data) => {
88+
console.log('Received message:', data);
89+
});
90+
91+
ws.on('close', () => {
92+
console.log('WebSocket connection closed');
93+
});
94+
```
95+
96+
## OutgoingMessage
97+
98+
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).
99+
100+
```js
101+
import { OutgoingMessage } from 'node:http';
102+
103+
const res = new OutgoingMessage();
104+
res.writeHead(200, { 'Content-Type': 'text/plain' });
105+
res.write('Hello, World!');
106+
res.end();
107+
```
108+
## IncomingMessage
109+
110+
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.
111+
112+
```js
113+
import { get, IncomingMessage } from 'node:http';
114+
import { ok, strictEqual } from 'node:assert/strict';
115+
116+
get('http://docs.cloudflare.com', (res) => {
117+
strictEqual(res.statusCode, 301);
118+
ok(res instanceof IncomingMessage);
119+
});
120+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
pcx_content_type: configuration
3+
title: https
4+
---
5+
6+
import { Render } from "~/components"
7+
8+
<Render file="nodejs-compat-howto" />
9+
10+
## Agent
11+
12+
An Agent manages HTTPS 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:https';
16+
import { strictEqual } from 'node:assert/strict';
17+
18+
const agent = new Agent();
19+
strictEqual(agent.protocol, 'https:');
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 HTTPS GET requests without manually configuring request options.
25+
26+
`get` methods behaves the same as `node:http` get method.
27+
28+
## request
29+
30+
The `request` 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.
31+
32+
`request` methods behaves the same as `node:http` request method.
33+

0 commit comments

Comments
 (0)