|
| 1 | +--- |
| 2 | +name: "Enable Node.js HTTP client modules" |
| 3 | +sort_date: "2025-08-15" |
| 4 | +enable_date: "2025-08-15" |
| 5 | +enable_flag: "enable_nodejs_http_modules" |
| 6 | +disable_flag: "disable_nodejs_http_modules" |
| 7 | +--- |
| 8 | + |
| 9 | +When you enable the `enable_nodejs_http_modules` compatibility flag and the [`nodejs_compat`](/workers/runtime-apis/nodejs/) |
| 10 | +flag is also enabled, the `node:http` and `node:https` modules become available for import. These modules provide |
| 11 | +client-side HTTP functionality for making outbound requests, but do not include Node.js HTTP server methods. |
| 12 | + |
| 13 | +```js |
| 14 | +import * as http from "node:http"; |
| 15 | +import * as https from "node:https"; |
| 16 | + |
| 17 | +// Make HTTP requests using Node.js APIs |
| 18 | +const options = { |
| 19 | + hostname: 'api.example.com', |
| 20 | + port: 443, |
| 21 | + path: '/data', |
| 22 | + method: 'GET' |
| 23 | +}; |
| 24 | + |
| 25 | +const req = https.request(options, (res) => { |
| 26 | + let data = ''; |
| 27 | + res.on('data', (chunk) => { |
| 28 | + data += chunk; |
| 29 | + }); |
| 30 | + res.on('end', () => { |
| 31 | + console.log('Response:', data); |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +req.end(); |
| 36 | +``` |
| 37 | + |
| 38 | +This enables compatibility with Node.js libraries and existing code that use the standard `node:http` and `node:https` |
| 39 | +APIs for making HTTP requests. The available functionality includes: |
| 40 | + |
| 41 | +- `http.request()` and `https.request()` for making HTTP/HTTPS requests |
| 42 | +- `http.get()` and `https.get()` for making GET requests |
| 43 | +- Request and response objects with standard Node.js APIs |
| 44 | +- Support for standard HTTP methods, headers, and options |
| 45 | + |
| 46 | +Server-side functionality such as `http.createServer()`, `http.Server`, and related server methods are not available, |
| 47 | +as Workers handle incoming requests through the [fetch event handler](/workers/runtime-apis/fetch/). |
0 commit comments