Skip to content

Commit 84d1b82

Browse files
committed
add node:http and node:https modules
1 parent b4fd35e commit 84d1b82

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
11+
## get
12+
13+
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.
14+
15+
`get` methods behaves the same as `node:http` get method.
16+
17+
## request
18+
19+
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.
20+
21+
`request` methods behaves the same as `node:http` request method.
22+

src/content/docs/workers/runtime-apis/nodejs/url.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
pcx_content_type: configuration
33
title: url
4-
54
---
65

76
import { Render } from "~/components"

0 commit comments

Comments
 (0)