7. Working with routes in ExpressJS
Page 46 | Listen in audio
Working with routes in ExpressJS is a crucial aspect of creating APIs in NodeJS. Routes are essentially the different paths an application can take, based on the URL and HTTP method it is requested. Routes in ExpressJS allow you to configure different responses to different URLs, allowing you to create a wide variety of functionality for your application.
Configuring Basic Routes
To start working with routes in ExpressJS, you first need to install and configure ExpressJS. Once you've done that, you can start defining routes. Here is an example of how to define a basic route:
const express = require('express');
const app = express();
app.get('/', function(req, res) {
res.send('Hello World!');
});
app.listen(3000, function() {
console.log('App listening on port 3000!');
});
In this example, we are defining a route to the base URL ("/") of our application. When a client requests this URL using the GET method, the server responds by sending "Hello World!".
Working with Route Parameters
ExpressJS allows you to define route parameters, which are URL segments that act as variables. You can define route parameters using a colon (":") followed by the parameter name. Here is an example:
app.get('/users/:userId', function(req, res) {
res.send('User ID is: ' + req.params.userId);
});
In this example, we define a route parameter called "userId". When a client requests a URL like "/users/123", the server responds with "User ID is: 123".
Working with Multiple HTTP Methods
ExpressJS supports all HTTP methods including GET, POST, PUT, DELETE and others. You can define routes for different HTTP methods as follows:
app.get('/users', function(req, res) {
res.send('Get a random user');
});
app.post('/users', function(req, res) {
res.send('Add a user');
});
app.put('/users/:userId', function(req, res) {
res.send('Update the user with the ID ' + req.params.userId);
});
app.delete('/users/:userId', function(req, res) {
res.send('Delete the user with the ID ' + req.params.userId);
});
With this set of routes, our application can respond to a variety of HTTP requests for the URL "/users".
Chaining Route Handlers
ExpressJS allows you to chain route handlers to a specific route. This is useful if you want to perform multiple functions for a specific route. Here is an example:
app.route('/book')
.get(function(req, res) {
res.send('Get a random book');
})
.post(function(req, res) {
res.send('Add a book');
})
.put(function(req, res) {
res.send('Update the book');
})
.delete(function(req, res) {
res.send('Delete the book');
});
In this example, we are chaining four route handlers to the URL "/book". Each handler responds to a different HTTP method.
Conclusion
Working with routes in ExpressJS is a fundamental aspect of creating APIs in NodeJS. Routes allow you to define your application's logic for different URLs and HTTP methods, allowing you to create a wide range of functionality for your application. With ExpressJS, you can define basic routes, work with route parameters, support multiple HTTP methods, and chain route handlers together.
Now answer the exercise about the content:
What is the role of routes in ExpressJS in creating APIs in NodeJS?
You are right! Congratulations, now go to the next page
You missed! Try again.
Next page of the Free Ebook: