Node.js routing without using Express.js

Ckmobile
4 min readJan 7, 2021

In this previous article, we already talked about how to return the html file to the browser.

Complete Node.js articles:

Part 1: How to create a HTTP server in Node.js

Part 2: How To Use the `req` Object in in Node.js

Part 3: How To Use the `res` Object in in Node.js

Part 4: How to render HTML in Node.js

Part 5: Node.js routing without using Express.js

Part 6: How to specify statusCode in Node.js

Part 7: How to redirect user’s browser URL to a different page in Nodejs?

Part 8: How to download and install a npm package globally in Node.js?

Part 9: What is package.json and how to install a npm package locally in Node.js?

But at the minute, no matter what URL we go to, whether it is just a forward slash “/”, or forward slash contact “/contact”, it always return the same HTML page.

Instead we want to send back a different page depends on the routes that the user visits.

There is a way to figure out the URL that the user request, base on the URL, the server return different HTML.

Full Course:

The first thing that we need to do is create other files under the views folder.

folder structure of the project

contact.html

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><h3>Contact</h3><p>Contact me </p>

--

--