CodingNeed.

2 · Build one skill at a time · 18 MIN

Receive an HTTP request and return JSON

A server maps a request method and path to a response.

An HTTP request contains a method such as GET and a URL path such as /health. A response supplies a status, headers and a body. Node’s createServer invokes a handler for each request. Write Content-Type so clients know the body is JSON, then end the response exactly once. The handler returns after the matching route, preventing a second response. The server listens only on the loopback address for local practice; this is not a production deployment or authentication boundary.

Start the server and open the printed URL. Inspect status and response headers in the browser network panel. Add the new route before the 404 fallback. Stop the process with Ctrl+C before the next HTTP lesson.

Before you start

Install Node.js 22.12+ (or a supported newer LTS). Create a folder named codingneed-node. Open a terminal in that folder. Save ONE lesson example as lesson.mjs and run node lesson.mjs. No npm dependencies are required. HTTP lessons keep running until you press Ctrl+C; stop the previous server before starting another.

File for this example: lesson.mjs

New words, explained

HTTP
The request/response protocol used by the web.
status
A numeric response result such as 200 or 404.
loopback
A network address that points to this computer.

Follow the example step by step

  1. Start the server and open the printed URL.
  2. Inspect status and response headers in the browser network panel.
  3. Add the new route before the 404 fallback.
  4. Stop the process with Ctrl+C before the next HTTP lesson.

You are ready to move on when: /health returns JSON with status 200. /topics returns a JSON array. An unknown path returns 404 instead of hanging.

Read the example

import {createServer} from 'node:http';
const server = createServer((req, res) => {
  const path = new URL(req.url, 'http://localhost').pathname;
  res.setHeader('Content-Type', 'application/json; charset=utf-8');
  if (req.method === 'GET' && path === '/health') {
    res.writeHead(200); res.end(JSON.stringify({ok:true})); return;
  }
  res.writeHead(404); res.end(JSON.stringify({error:'Not found'}));
});
server.listen(3000, '127.0.0.1', () => console.log('Open http://127.0.0.1:3000/health'));
Check the expected output
GET /health returns status 200 and {"ok":true}. An unknown path returns 404. Stop the server with Ctrl+C.

Your challenge

Add GET /topics returning a JSON array with React, SQL and Node.js. Keep the unknown-route response.

Solution cost: Discuss the operations in this small example; rendering and I/O costs depend on the host. time · Proportional to the example’s retained data. space

Common trap

Forgetting res.end leaves the request waiting. Running two servers on port 3000 causes an address-in-use error.

Study the project implementation
import {createServer} from 'node:http';
const server = createServer((req, res) => {
  const path = new URL(req.url, 'http://localhost').pathname;
  res.setHeader('Content-Type', 'application/json; charset=utf-8');
  if (req.method === 'GET' && path === '/health') {
    res.writeHead(200); res.end(JSON.stringify({ok:true})); return;
  }
  res.writeHead(404); res.end(JSON.stringify({error:'Not found'}));
});
server.listen(3000, '127.0.0.1', () => console.log('Open http://127.0.0.1:3000/health'));

Further reading: Official documentation

Next lesson: Validate a query parameter at the boundary

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.