When working with Node.js, one of the core capabilities it offers is to create web servers from scratch using native modules β no frameworks like Express needed (though they help a lot!).
In this blog, weβll explore three key core modules:
- http β build your own basic web server
- https β build secure servers using SSL/TLS
- url β parse and manipulate URLs like a pro
Weβll also compare these native approaches with Express.js to see why frameworks matter.
π¦ 1. The HTTP Module β Your First Node.js Server
Node.js provides a built-in http module that allows you to spin up a web server without using Express or any third-party library.
π§ Concept
An HTTP server listens for incoming requests on a port.
It then sends back responses based on the request URL or method.
π‘ Basic Example
const http = require('http');
const server = http.createServer((req, res) => {
if (req.url === '/') {
res.end('Welcome to the Home Page');
} else if (req.url === '/about') {
res.end('About Page');
} else {
res.end('404 Not Found');
}
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});
π Breakdown
http.createServer(callback) β sets up a server
req.url β gives the URL path
res.end() β ends the response
π 2. The HTTPS Module β Secure Your Communication
While http is great, itβs not secure. Thatβs where https comes in.
β
Why Use HTTPS?
Encrypts communication (SSL/TLS)
Mandatory for handling passwords, payments, etc.
Required in production environments
π Prerequisite: SSL Certificates
You need:
- A private key (key.pem)
- A certificate (cert.pem) Generate self-signed ones for testing:
openssl req -nodes -new -x509 -keyout key.pem -out cert.pem
π‘ HTTPS Server Example
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('Hello from Secure HTTPS Server!');
}).listen(4433, () => {
console.log('HTTPS Server running on https://localhost:4433');
});
π 3. The URL Module β Parsing Made Simple
Want to extract ?term=react or /products from incoming requests? Thatβs what the url module is for.
π‘ Modern URL Parsing
const { URL } = require('url');
const myURL = new URL('https://example.com/products?category=books&id=101');
console.log(myURL.hostname); // example.com
console.log(myURL.pathname); // /products
console.log(myURL.searchParams.get('category')); // books
π Comparing: Native https vs Express
Letβs see how things differ between native Node.js and Express.
π§ͺ Task: Parse /search?term=react
Using Native HTTPS:
const https = require('https');
const fs = require('fs');
const { URL } = require('url');
const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
https.createServer(options, (req, res) => {
const fullUrl = `https://${req.headers.host}${req.url}`;
const parsedUrl = new URL(fullUrl);
if (parsedUrl.pathname === '/search') {
const term = parsedUrl.searchParams.get('term') || 'none';
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Search term received', term }));
} else {
res.writeHead(404);
res.end('404 Not Found');
}
}).listen(4433);
Using Express (Much Simpler!)
const express = require('express');
const https = require('https');
const fs = require('fs');
const app = express();
const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
app.get('/search', (req, res) => {
const term = req.query.term || 'none';
res.status(200).json({ message: 'Search term received', term });
});
https.createServer(options, app).listen(4433, () => {
console.log('Express HTTPS server running at https://localhost:4433');
});
π§ Bonus: Middleware Example in Express
const morgan = require('morgan');
app.use(morgan('dev')); // Logs every incoming request
In native https, you'd have to code logging, parsing, etc., all manually!
π― Conclusion
Understanding how Node.js works at the core level gives you control and insight into what frameworks like Express abstract away. Hereβs what we learned:
Use http or https for learning or small utilities.
For production-grade apps, always use https with SSL.
Use the modern URL class for parsing.
Express makes development faster, cleaner, and more scalable.
π§ͺ Try It Yourself
β
Generate SSL certs
β
Build both native and Express-based HTTPS servers
β
Parse query parameters from /search?term=yourvalue
Then⦠compare your code and see why Express is a game-changer!
Top comments (0)