Node.js HTTP Server
An HTTP server receives requests and produces responses. Begin with a listener on loopback and an operating-system-selected port so the lesson cannot collide with your website.
How it works
The complete source starts its own server, makes one actual local request, checks the response and closes the listener in finally. Port zero asks the operating system for an available port; it is not the port a browser should guess. A readable lesson should state its address scope and resource lifetime. This fixture is temporary and intentionally does not expose a public interface or change the Laravel server on port 8000.
Run the code
Run this example locally with Node.js. The exact sources are reviewed on Node 22.15.1 and Node 24.22.1 unless a specialised lab states otherwise. Save the complete source as http-server.mjs in a separate empty exercises folder and run node http-server.mjs. There is no Node.js browser runner on Bhylu; the plain JavaScript playground does not provide these filesystem, server or process APIs.
Download the complete source. Ordinary Node scripts need no Apache, PHP or MySQL service. File examples own their temporary folders; network fixtures use loopback and available temporary ports.
Example
import http from 'node:http';
import { once } from 'node:events';
const server = http.createServer((request, response) => {
response.writeHead(200, { 'content-type': 'text/plain; charset=utf-8' });
response.end('Field-kit desk ready');
});
server.listen(0, '127.0.0.1');
await once(server, 'listening');
const base = 'http://127.0.0.1:' + server.address().port;
try {
const response = await fetch(base);
console.log(response.status);
console.log(await response.text());
} finally {
server.closeAllConnections();
await new Promise((resolve, reject) =>
server.close((error) => (error ? reject(error) : resolve())),
);
}
Expected result
200 Field-kit desk ready
The client sees a successful status and the exact intended text over a real loopback connection. Reading the response body lets the client finish its side of the exchange before the fixture shuts down. This is not a public deployment or TLS test.
Use the Node.js API reference for the precise version contract.