Create an HTTP Server with Built-in Node.js Modules

Start a local HTTP service and understand request objects, response objects, and listening ports

🎯 Introduction

By the end of this article, you will be able to start a local HTTP service and understand request objects, response objects, and listening ports.

🧱 Write a request handler first

Create hello.mjs in backend-demo. This complete function returns text when it receives a request:

hello.mjs
export function hello(req, res) {
    res.setHeader('Content-Type', 'text/plain; charset=utf-8');
    res.end('Hello, Node.js');
}

req describes this request; res provides the interface for this response. end sends the body and ends the response. Without it, the client may keep waiting. Every address returns the same text here, so first confirm that the service receives requests.


🔌 Create the server, then listen on a port

Create hello-server.mjs, import the handler, and create a server object:

hello-server.mjs
import { createServer } from 'node:http';
import { hello } from './hello.mjs';

const server = createServer(hello);

createServer calls hello for each request. It returns a server object, but the server is not yet accepting network connections.

Continue in the same file by adding a listener:

hello-server.mjs
server.listen(3000, '127.0.0.1', () => {
    console.log('http://127.0.0.1:3000');
});

3000 is the listening port. 127.0.0.1 permits access only from this computer, which suits these exercises. The callback indicates that listening succeeded; it does not mean every endpoint has passed its tests.


🧪 Verify the result from a client

Run the server:

node hello-server.mjs

Open another terminal and request it. curl is a terminal HTTP client; -i also displays response headers:

curl -i http://127.0.0.1:3000

You should see 200 OK, a text Content-Type, and the body “Hello, Node.js”. Visiting the same address in a browser displays the same message.

The service keeps waiting for requests rather than exiting after one response. Stop and restart it after editing the code, or use development watch mode:

node --watch hello-server.mjs

Node.js restarts the process when files change. This is useful during development; it does not provide production crash recovery.


⚠️ Distinguish startup failures from request failures

EADDRINUSE means another program is using the port, often because a previous example is still running. Press Ctrl+C in its terminal to stop it, then start the current example.

An error response after the server starts is a different problem: inspect the handler. Changing ports does not resolve business logic errors. Do not run multiple course examples on port 3000 simultaneously.


❓ Questions and Answers

Q1: Does each request start a new Node.js process?

No. The same server process handles multiple requests, each with its own req and res.

Q2: Why can't my phone access this address?

127.0.0.1 refers to the device itself, and this server listens only locally. Access from another device requires a suitable listening address and network configuration.

Q3: Can I send another response after res.end?

No. Each request has one response. End the current function's control flow after handling it to prevent later code from writing again.


🧪 Practice

Change the body to your nickname, restart, and verify it with both a browser and curl. Then stop the service and observe a connection failure rather than HTTP 404.

// Write your code or record your verification results here