Create HTTP Web Server in Node.js
Node.js has a built-in http module for creating servers. In practice, most projects use Express instead, but understanding the bare http module helps you see what Express is doing underneath.
Creating a server
Import the http module and call http.createServer(). It takes a callback that runs on every request.
const http = require('http');
const server = http.createServer((req, res) => {
console.log('request received');
});
Then tell the server to start listening:
server.listen(3000, 'localhost', () => {
console.log('listening on http://localhost:3000');
});
The response object
To send a response, set a header describing the content type, write the body, and end the response.
Plain text:
const http = require('http');
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/plain');
res.write('Hello, world');
res.end();
});
server.listen(3000, 'localhost', () => {
console.log('listening on http://localhost:3000');
});
HTML:
const http = require('http');
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/html');
res.write('<p>Hello, world</p>');
res.write('<p>Hello, again</p>');
res.end();
});
server.listen(3000, 'localhost', () => {
console.log('listening on http://localhost:3000');
});
Serving HTML files
Use the fs module to read an HTML file and send it as the response.
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/html');
fs.readFile('./views/index.html', (err, data) => {
if (err) {
console.log(err);
res.end();
} else {
res.end(data);
}
});
});
server.listen(3000, 'localhost', () => {
console.log('listening on http://localhost:3000');
});
Basic routing
Use a switch statement on req.url to serve different pages.
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/html');
let filePath = './views/';
switch (req.url) {
case '/':
filePath += 'index.html';
res.statusCode = 200;
break;
case '/about':
filePath += 'about.html';
res.statusCode = 200;
break;
default:
filePath += '404.html';
res.statusCode = 404;
}
fs.readFile(filePath, (err, data) => {
if (err) {
console.log(err);
res.end();
}
res.end(data);
});
});
server.listen(3000, 'localhost', () => {
console.log('listening on http://localhost:3000');
});
Status codes
The status code describes the type of response. Set it with res.statusCode:
| Range | Meaning |
|---|---|
| 100s | Informational |
| 200s | Success |
| 300s | Redirects |
| 400s | Client errors |
| 500s | Server errors |
Common codes: 200 (OK), 301 (Moved Permanently), 404 (Not Found), 500 (Internal Server Error).
Redirects
Use setHeader('Location', ...) with a 301 status to redirect a request:
case '/about-us':
res.statusCode = 301;
res.setHeader('Location', '/about');
res.end();
break;
Next steps
For real web apps and APIs, use Express
. It handles routing, middleware, and JSON responses with far less boilerplate than the raw http module.
What to read next
- Express
: the standard web framework built on top of
http - Core Modules : overview of all built-in modules