June 22, 2026 · by Meegrow Labs

Adding Routes in Node.js: How One Server Serves Many Pages

Right now your server probably gives the same reply to everyone. Visit any address, and you get one answer. But real websites are not like that. They have a home page, an about page, maybe a menu page. So how does a single server know what to show for each one?

The answer is routes. In this lesson we will look at adding routes in Node.js, the simple idea that lets one server hold a whole website.

Adding routes in Node.js means telling your server which reply to send for each URL a visitor asks for, so one server can serve a home page, an about page, and many others, each with its own clean address.

What is a route, really?

Think of a bank. It is one building, but it has many counters. Each counter does one specific job, one for deposits, one for loans, one for new accounts.

A web server works the same way. One server answers every visitor. But each route handles one address. A route is simply a rule that decides what to send when someone visits a particular URL.

So / can send your home page, and /about can send your about page, all from the same running server.

How do I add a route in code?

The idea is short. You check which address the visitor asked for, then send back the matching page. Here is the core of it:

if (url === '/about') {
  send(aboutPage);
}

Let’s read that slowly. /about is the address the visitor typed. send(aboutPage) is the reply your server gives back. That is one route: one address, one reply.

You write one route per page. / for the home page. /about for the about page. /menu for the menu. Each line maps one address to one response.

Why bother adding routes at all?

Because without routes, your server is stuck saying one thing forever. With routes, that same server can hold an entire website, with clean and simple URLs that visitors can actually understand and share.

This is exactly how the websites you use every day behave. When you open a shopping app and tap a product, the address changes and a new page loads, all from the same backend. Routes are what make that possible.

How do I try adding routes right now?

If you have been following along with the earlier Node.js lessons, your server file is ready. Add a route for /about, then start the server:

node server.js

Now open your browser and visit two addresses on your own machine:

  • localhost:3000/ — this should show your home reply.
  • localhost:3000/about — this should show your about reply.

Each address answers differently. That small difference is the whole point: you have just turned one server into a multi-page website. If you are still shaky on what localhost means, revisit the previous lesson on running your server before you continue.

What comes after routes?

Once your server can serve different pages, the next natural step is sending data instead of just pages. That is serving JSON, which is how apps and APIs pass information back and forth. We cover it in the very next lesson.

If you are starting from zero, you do not have to figure this out alone. Adding routes is one small step inside the free Zero to AI Hero course, which walks you from “what’s a file?” all the way to building real apps and AI agents, in plain Hindi and English.

Key takeaways

  • A route is a rule that decides what your server sends for a given URL.
  • One server, many counters: like a bank, a single server can answer many addresses, each with its own job.
  • Adding routes in Node.js means writing one route per page, mapping each address to one reply.
  • Test it by running node server.js and visiting / and /about on localhost.
  • Next up is serving JSON, so your server can send data, not just pages.

🚀 Take the full free course: Zero to AI Hero — learn to build with AI from scratch. New lessons daily, in Hindi & English.

Meegrow Labs

We help India go from zero to AI hero — learn to use & build with AI from scratch, in Hindi & English. Start the free course →

Want to actually learn this?

151 free 2-minute lessons — from "what's a file?" to building with AI.

▶ Start the free course