Code Reference

HTTP (Node)

JavaScript · Reference cheat sheet

HTTP (Node)

JavaScript · Reference cheat sheet


📋 Overview

Node’s node:http / node:https build servers and clients. For most apps prefer frameworks (Express, Fastify) or fetch (Node 18+). This sheet covers the core APIs.

🔧 Core concepts

APIRole
http.createServer(handler)HTTP server
req / resIncomingMessage / ServerResponse
http.request / getOutgoing client
httpsTLS variants
fetch(url)Modern client (undici)

💡 Examples

Tiny server:

import http from 'node:http';

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ ok: true, url: req.url }));
});

server.listen(9000, () => console.log('http://127.0.0.1:9000'));

fetch client (Node 18+):

const res = await fetch('https://httpbin.org/get');
const data = await res.json();
console.log(data.url);

⚠️ Pitfalls

  • Always handle error on sockets; unhandled errors crash the process.
  • Remember to res.end() or the request hangs.
  • Prefer fetch / undici over hand-rolled http.request for clients.
  • Binding 0.0.0.0 exposes the service on all interfaces.

On this page