Skip to main content

Routes

Here is a comprehensive review of the routing functionality provided by Express, a popular web framework for Node.js:

Defining routes In Express, you can define routes using the various routing methods provided by the framework, such as app.get(), app.post(), app.put(), app.delete(), etc. These methods take two arguments: a path and a callback function. The callback function will be executed when the specified path is requested using the corresponding HTTP method.

For example, the following code defines a route that responds to a GET request to the / path with a "Hello World" message:

app.get('/', (req, res) => {
res.send('Hello World');
});

Route parameters​

You can include parameters in your routes by using a colon followed by the parameter name. For example:

app.get('/users/:id', (req, res) => {
const { id } = req.params;
res.send(`User ID: ${id}`);
});

In this case, the id parameter will be extracted from the request parameters and sent as part of the response.

Route middleware​

You can use route middleware to add functionality to your routes. Route middleware is a function that is executed before the route callback function. It can be used to perform tasks such as authentication, authorization,