DEV Community

Kumar Ayush
Kumar Ayush

Posted on

🌐 Mastering HTTP, HTTPS & URL Modules in Node.js β€” The Foundation of Web Servers

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
Enter fullscreen mode Exit fullscreen mode

πŸ” 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');
});
Enter fullscreen mode Exit fullscreen mode

🌐 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
Enter fullscreen mode Exit fullscreen mode

πŸ” 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);
Enter fullscreen mode Exit fullscreen mode

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');
});
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Bonus: Middleware Example in Express

const morgan = require('morgan');
app.use(morgan('dev')); // Logs every incoming request
Enter fullscreen mode Exit fullscreen mode

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)