TLDR

Introduction
Routing is an essential building box for building web applications. It is the web application’s response for a specific URI (or a path) endpoint that is requested from the front end.
Based on the HTTP method the application will respond in a certain way based on the backend logic.
It simply responds to what needs to happen when the user hits a specific endpoint.
In this blog post, we’ll see how to set up an express route, advance techniques, and best practices to use express routing for more scalable, maintainable web applications.
Setting up a simple express route
const express = require('express');
const app = express();
// Define a route handler for the root URL ('/')
app.get('/', (req, res) => {
res.send('Hello world!');
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
In this code block, we are creating a simple express application that listens on port 3000 and responds with “Hello world!” when the root URL is accessed.
// posts.js - handling post-related API calls
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
// Implement logic to retrieve all posts from the database
res.send('All posts retrieved');
});
router.get('/:postId', (req, res) => {
const postId = req.params.postId;
// Implement logic to retrieve post by ID from database
res.send(`Post with ID ${postId} retrieved`);
});
router.post('/', (req, res) => {
// Implement logic to create new post in the database
res.send('New post created');
});
router.put('/:postId', (req, res) => {
const postId = req.params.postId;
// Implement logic to update post by ID in database
res.send(`Post with ID ${postId} updated`);
});
router.delete('/:postId', (req, res) => {
const postId = req.params.postId;
// Implement logic to delete post by ID from database
res.send(`Post with ID ${postId} deleted`);
});
module.exports = router;
In this code block, we are creating a simple express router that handles the CRUD operations for posts. We define routes for getting all posts, getting a post by ID, creating a new post, updating a post by ID, and deleting a post by ID.
// server.js - main server file
const express = require('express');
const app = express();
const postsRouter = require('./posts'); // Import the posts router
const port = 3000;
app.use(express.json()); // Middleware to parse JSON request bodies
app.use('/posts', postsRouter); // Use the posts router for routes starting with /posts
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
In this code block, we are importing the posts router and using it for routes that start with /posts. This allows us to handle all post-related API calls in a modular way.
Conclusion
In this blog post, we have seen how to set up a simple express route and how to create a modular router for handling post-related API calls. We have also learned about the importance of routing in web applications and how it can help us build scalable and maintainable applications.
Additional Resources
Additional Context
Feel free to modify the code as per your requirements and explore more advanced routing techniques in Express.js
Happy coding! 😎