Before web frameworks like Express or Fastify existed, developers built web servers using Node.js's built-in http and https modules. Understanding how pure HTTP servers handle routing, HTTP headers, status codes, and request streams will make you a much stronger backend engineer.
The following example demonstrates routing, query parameter parsing, and POST payload handling without any external third-party NPM packages:
// pure-http-server.js
const http = require('http');
const { URL } = require('url');
const PORT = 3000;
const server = http.createServer((req, res) => {
// Parse full URL and search parameters
const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
const path = parsedUrl.pathname;
const method = req.method;
// Helper to send JSON responses
const sendJson = (statusCode, data) => {
res.writeHead(statusCode, {
'Content-Type': 'application/json',
'X-Powered-By': 'Veewom Pure Node.js Engine'
});
res.end(JSON.stringify(data, null, 2));
};
// Route 1: GET /
if (method === 'GET' && path === '/') {
return sendJson(200, {
message: 'Welcome to Veewom Pure HTTP Server API',
status: 'active'
});
}
// Route 2: GET /api/greet?name=Rahul
if (method === 'GET' && path === '/api/greet') {
const name = parsedUrl.searchParams.get('name') || 'Guest';
return sendJson(200, { greeting: `Hello, ${name}!` });
}
// Route 3: POST /api/feedback (Handling Incoming Stream Body)
if (method === 'POST' && path === '/api/feedback') {
let bodyChunks = [];
// Collect streaming data chunks
req.on('data', (chunk) => {
bodyChunks.push(chunk);
});
// Entire stream received
req.on('end', () => {
try {
const bodyBuffer = Buffer.concat(bodyChunks);
const parsedBody = JSON.parse(bodyBuffer.toString('utf8'));
console.log('Received feedback:', parsedBody);
return sendJson(201, {
success: true,
message: 'Feedback received successfully!',
dataReceived: parsedBody
});
} catch (err) {
return sendJson(400, { error: 'Invalid JSON payload received' });
}
});
return; // Don't end response prematurely!
}
// Fallback 404 Not Found
return sendJson(404, { error: 'Route not found' });
});
server.listen(PORT, () => {
console.log(`Pure HTTP server running at http://localhost:${PORT}/`);
});
In production, all traffic must be encrypted over HTTPS. The https module accepts your SSL certificates generated by Let's Encrypt or OpenSSL:
const https = require('https');
const fs = require('fs');
const sslOptions = {
key: fs.readFileSync('certs/privkey.pem'),
cert: fs.readFileSync('certs/fullchain.pem')
};
https.createServer(sslOptions, (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Secure HTTPS connection established with Veewom server!
');
}).listen(8443, () => {
console.log('Secure HTTPS server live on port 8443');
});
Boost your server-side JavaScript proficiency by studying these closely connected core modules:
Parse URLs and query parameters cleanly with WHATWG API.
Transition from pure HTTP to modern Express frameworks.
Basic HTTP server setup walkthrough.