Import & Create Server

import http from "node:http";

const server = http.createServer((req, res) => {
  // handle request
});

server.listen(3000, () => {
  console.log("Server running on port 3000");
});

Request Object (req)

const { method, url, headers } = req;

Response Object (res)

res.writeHead(statusCode, headers)

Sets status code and headers. Must be called before res.end(). Use writeHead() to explicitly send status and headers

res.writeHead(200, { "Content-Type": "application/json" });

res.setHeader(name, value)

Sets a single header. Can be called multiple times before res.end().

res.setHeader("Content-Type", "application/json");

res.statusCode = code