Build a basic HTTP server
Expose a lightweight HTTP endpoint without adding a web framework.
Node.js · Module
Node.js's node:http module provides low-level HTTP client and server APIs. It streams request and response data instead of buffering complete messages, leaving application-level parsing and routing to the caller.
The node:http module exposes low-level client and server APIs. It parses HTTP messages into headers and bodies while using streams rather than buffering complete requests or responses, so applications handle routing, body interpretation, and other higher-level behavior themselves.
import * as http from 'node:http';
const server = http.createServer(requestListener);
const request = http.request(options[, callback]);
const getRequest = http.get(options[, callback]);| Name | Type | Description |
|---|---|---|
requestListener | Function | The request listener invoked for incoming requests; it receives an IncomingMessage and ServerResponse. |
The module namespace provides HTTP client and server APIs; http.createServer() returns an HTTP server, while http.get() returns an http.ClientRequest.
node:http built-in specifier with either CommonJS or ES module syntax.import * as http from 'node:http';
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, world!\n');
});
server.listen(3000, () => {
console.log('Listening on http://localhost:3000');
});Creates a basic HTTP server, writes a response status and header, then ends each response.
Expose a lightweight HTTP endpoint without adding a web framework.
Send outbound HTTP requests to another service with explicit request control.
Process large or chunked payloads incrementally.
Create framework or infrastructure primitives around Node's protocol-level APIs.
http.createServer() provide?http.createServer() creates an HTTP server and accepts a request listener. The listener receives the incoming request and server response objects, allowing the application to inspect the request and write the response.
Follow-up: Why is the HTTP API described as low-level? It handles stream processing and message parsing, but leaves application-level interpretation and routing to the user.
Node's HTTP interface avoids buffering an entire request or response, allowing large or chunked messages to be streamed. This reduces the need to hold complete payloads in memory.
Follow-up: What trade-off follows from streaming? Handlers must correctly consume and produce stream data rather than assuming the complete body is immediately available.
http.get() differ from http.request()?http.get() is a convenience method for common GET requests. It sets the method to GET and invokes req.end() automatically, while http.request() is used when more control is needed.
Follow-up: When should http.request() be preferred? Use it when the method, request body, headers, or request lifecycle need explicit control.
http.createServerhttp.requesthttp.gethttp.Serverhttp.IncomingMessagehttp.ServerResponsehttp.Agent