Backend APIs · 9 min read · Intermediate
HTTP Servers & JSON Routing
Build zero-dependency HTTP APIs with node:http, parse URL paths cleanly, and return proper HTTP status codes.
What you'll learn
- Create an HTTP request handler with http.createServer()
- Parse query parameters safely with new URL(req.url, base)
- Return JSON with Content-Type: application/json and accurate status codes
Frameworks like Express, Fastify, and Hono all build on top of Node’s built-in node:http module. Understanding req.method, req.url, res.writeHead(status, headers), and res.end(body) demystifies every Node web framework.
Fill-in-the-Blank Exercise
Send JSON Response
Set status 200 with JSON content-type and finish the response.
res.writeHead(200, { "Content-Type": "" });
res.(JSON.stringify({ ok: true }));Live Code Lab (Node.js 20)
Node.jsrouter_demo.js
Expected OutputClick “Run Live” to compile with Node.js 20
{ status: 200, body: { filter: 'node', ok: true } }
{ status: 404, body: { error: 'Not found' } }Check Your Understanding
Score: 0 / 1
1. Which HTTP status code is best when a POST request successfully creates a new resource?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.