Node.js is a popular tool for creating scalable and high-performance web applications. One of its most popular modules is Express. Express is a minimal and flexible Node.js web application framework that provides robust routing services to help you build APIs and web applications quickly.
Routing is an essential aspect of any web application that involves taking a URL request and directing it to the appropriate functionality. In this article, we will be discussing how to use Express routing within Node.js and a special note on how to set up a 404 page.
Getting Started with Express Routing
To get started with Express routing, we first need to install the Express module using npm. Open your command prompt or terminal and type in the following command:
npm install express --save
Once the module is installed, we need to create an Express application instance. Open your text editor and create a new file called app.js. Add the following code to create an instance of the Express module:
const express = require('express');
const app = express();
The above code creates an instance of the Express module and initializes it in the app variable.
Before we dive deeper into routing, let's first initialize our application on a specific port. Add the following code to your app.js file:
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
The above code sets the port for the application to run and initiates it to listen for incoming requests.
Creating Routes in Express
Now that we have our application ready, let's define our first route. The most basic route we can create in Express is a GET request. Add the following code to your app.js file:
app.get('/', (req, res) => {
res.send('Hello World!');
});
The above code creates a route that sends 'Hello World!' as a response when the application receives a GET request on the root URL ('/') of the application.
We can also define routes for specific URLs, parameters, and methods. Let's create a route that takes a dynamic parameter and responds with a message. Add the following code to your app.js file:
app.get('/greet/:name', (req, res) => {
const name = req.params.name;
res.send(`Hello ${name}!`);
});
The above code defines a route that takes a dynamic parameter, in this case, a name, and responds with a greeting message that includes the name.
Setting Up a 404 Page
By default, Express does not handle 404 (page not found) errors. It is essential to create a custom 404 page to alert users when they try to access a route that does not exist.
To create a custom 404 page, add the following code to your app.js file:
app.use((req, res, next) => {
res.status(404).send("Sorry, the page you're looking for does not exist.");
});
The above code defines a middleware function that sets the status code to 404 and sends a message, 'Sorry, the page you're looking for does not exist.'
Conclusion
Express is a powerful Node.js web application framework that simplifies the process of routing and managing requests. In this article, we covered the basics of how to use Express routing within Node.js and how to create a custom 404 page. By following the guidelines mentioned, developers can create efficient and robust applications that efficiently handle incoming requests and maintain clean code.