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.
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
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.
In this example:
:idis a dynamic parameterreq.params.idretrieves the value from the URL
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.
Query Parameters
Query parameters are used to pass optional data in the URL. They are accessed using req.query.
Example:
URL: /search?name=Rahul → Response: Rahul
Multiple Routes
You can define multiple routes in your application to handle different functionalities.
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.
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().
Then use it in your main app:
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.
app.get().

