In this tutorial, we aim to shed more light on the concepts of routing and middleware in Node.js. Routing refers to how an application's endpoints (URIs) respond to client requests. Middleware, on the other hand, are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle.
By the end of this tutorial, you'll be able to:
- Understand how routing works in Node.js
- Implement basic routing in your application
- Understand what middleware is and why it's useful
- Implement middleware functions in your application
The prerequisites for this tutorial are basic knowledge of JavaScript, Node.js, and Express.js.
Routing defines how an application responds to a client request to a particular endpoint. For example, a client may send a GET request to the /home endpoint, and we need to define how our application responds to that request.
Let's create a simple route:
app.get('/home', function(req, res) {
res.send('Welcome to the Home Page!');
});
In this example, we're defining a route for the /home endpoint. When a GET request is made to this endpoint, the function is triggered, sending the string 'Welcome to the Home Page!' back to the client.
Middleware functions are functions that have access to the request object, the response object, and the next function in the application's request-response cycle. Here's a basic example:
app.use(function(req, res, next) {
console.log('Time:', Date.now());
next();
});
In this example, we're using app.use() to apply a middleware function to every request made to the app. This function simply logs the current time and then calls the next function in the cycle.
const express = require('express');
const app = express();
// define a route handler for the default home page
app.get('/', (req, res) => {
res.send("Hello, World!");
});
// start the Express server
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
When we run this code and navigate to http://localhost:3000 in a browser, we should see the text "Hello, World!".
const express = require('express');
const app = express();
// middleware that logs the time of every request
app.use((req, res, next) => {
console.log(`Time: ${Date.now()}`);
next();
});
// route handler for the default home page
app.get('/', (req, res) => {
res.send('Hello, World!');
});
// start the Express server
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Every time a request is made to the server, the middleware function will log the current time to the console before the request is passed on to the route handler.
In this tutorial, we've learned about routing and middleware in Node.js. We've seen how to define routes to respond to specific client requests, and how to use middleware to add functionality to our application.
For further learning, you might want to look into more complex routing techniques, like parameterised routes and route handlers. For middleware, consider learning more about error-handling middleware and built-in middleware.
Here's a possible solution for exercise 1:
const express = require('express');
const app = express();
app.get('/about', (req, res) => {
res.send('About Page');
});
app.get('/contact', (req, res) => {
res.send('Contact Page');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
And here's one for exercise 2:
const express = require('express');
const app = express();
app.use((req, res, next) => {
console.log(`Date: ${Date.now()} - Route: ${req.path}`);
next();
});
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Remember, practice makes perfect. Keep experimenting with different routes and middleware functions to get a good grasp of these concepts!