Routing in Express

Node js 8 min min read Updated: Mar 30, 2026 Intermediate
Routing in Express
Intermediate Topic 2 of 10

Routing in Express.js

Routing is one of the core features of Express.js. It defines how your application responds to client requests for specific URLs and HTTP methods.

In simple terms, routing decides what should happen when a user visits a particular endpoint like /users or /products. It connects incoming requests to the appropriate logic in your application.

Key Concept: Routes map URLs and HTTP methods (GET, POST, PUT, DELETE) to specific functions in your application.

What is Routing?

Routing refers to defining endpoints (URLs) and specifying how the server should respond when those endpoints are accessed.

Express provides simple methods like app.get(), app.post(), app.put(), and app.delete() to define routes.

Basic Route Example

javascript app.get("/", (req, res) => { res.send("Home Page"); });

This route handles GET requests to the root URL and sends a response back to the client.

Route with Parameters

Route parameters allow you to capture dynamic values from the URL. These values can be accessed using req.params.

javascript app.get("/users/:id",(req,res)=>{ res.send(req.params.id); });

In this example:

  • :id is a dynamic parameter
  • req.params.id retrieves the value from the URL
Output

URL: /users/101 → Response: 101

Handling Different HTTP Methods

Express supports multiple HTTP methods for different operations:

  • GET: The GET method is used to request and retrieve data from the server. It does not modify any data and is commonly used to fetch information such as user details, products, or pages.
  • POST: The POST method is used to send data to the server to create a new resource. It is commonly used when submitting forms or adding new records to a database.
  • PUT: The PUT method is used to update an existing resource on the server. It replaces the entire resource with new data provided in the request.
  • DELETE: The DELETE method is used to remove a resource from the server. It is typically used to delete records such as users, products, or files.
javascript app.post("/users", (req, res) => { res.send("User created"); }); app.put("/users/:id", (req, res) => { res.send("User updated"); }); app.delete("/users/:id", (req, res) => { res.send("User deleted"); });

Query Parameters

Query parameters are used to pass optional data in the URL. They are accessed using req.query.

javascript app.get("/search", (req, res) => { res.send(req.query.name); });

Example:

Output

URL: /search?name=Rahul → Response: Rahul

Multiple Routes

You can define multiple routes in your application to handle different functionalities.

javascript app.get("/about", (req, res) => { res.send("About Page"); }); app.get("/contact", (req, res) => { res.send("Contact Page"); });

Route Order Matters

Express matches routes in the order they are defined. If a route is defined earlier, it will be executed before later routes.

javascript app.get("/users/:id", (req, res) => { res.send("Dynamic route"); }); app.get("/users/profile", (req, res) => { res.send("Profile page"); });

In this case, /users/profile may be treated as a dynamic route unless defined before.

Using Express Router

For larger applications, routes should be organized into separate files using express.Router().

javascript const express = require("express"); const router = express.Router(); router.get("/", (req, res) => { res.send("Users List"); }); module.exports = router;

Then use it in your main app:

javascript const userRoutes = require("./routes/users"); app.use("/users", userRoutes);

Real-World Use Cases

  • Building REST APIs
  • Handling user authentication routes
  • Serving different pages in web apps
  • Microservices routing

Best Practices

  • Organize routes into separate files
  • Use meaningful route names
  • Handle errors properly
  • Validate route parameters

Common Mistakes

  • Incorrect route order
  • Not validating parameters
  • Mixing business logic with routing logic

Conclusion

Routing is the backbone of any Express.js application. It connects client requests to server logic and helps structure your backend efficiently.

By mastering routing, you can build scalable APIs and organize your application in a clean and maintainable way.

Quick Summary: Routing in Express defines how your application handles different URLs and HTTP methods using functions like app.get().

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators