http.IncomingMessage
The request object on the server (and response object on the client) - a readable stream.
Syntax
(req) => { req.method; req.url; req.headers } Returns
Readable — A readable stream with method, url, headers, and statusCode.
Examples
import { createServer } from 'node:http';
createServer((req, res) => {
console.log(req.method, req.url);
console.log(req.headers['user-agent']);
res.end();
}).listen(3000);
Output
GET /users
curl/8.4.0
createServer((req, res) => {
let body = '';
req.on('data', (c) => (body += c));
req.on('end', () => {
res.end(`received ${body.length} bytes`);
});
}).listen(3000);
Output
received 27 bytes
Notes
Header names are lowercased. The body is a stream and must be
consumed; on the server cap its size to prevent memory-exhaustion
attacks. `req.url` is only the path and query, not the full URL.