Article image Working with routes in ExpressJS: Passing parameters in routes

7.2. Working with routes in ExpressJS: Passing parameters in routes

Page 48 | Listen in audio

7.2. Working with routes in ExpressJS: Passing parameters in routes

Routes are a fundamental part of developing APIs in NodeJS using ExpressJS. They define the entry points to your application and how they should respond to different types of HTTP requests. In this section, we'll explore how to work with routes in ExpressJS, with a special focus on how to pass parameters in routes.

Understanding Routes

In ExpressJS, a route is a defined path in your application. Each route can have one or more function handlers, which are executed when the route is matched. Routing is how ExpressJS decides which handler function to run for a given HTTP request.

Defining Routes

To define a route in ExpressJS, you use methods on the application object that correspond to HTTP verbs. For example, for a GET route, you would use app.get(). For a POST route, you would use app.post(). Each of these methods accepts two arguments: the route string and the handler function.

Here is an example of how to define a simple GET route:

app.get('/example', function(req, res) {
  res.send('Hello, world!');
});

In this example, the route '/example' will match any GET request to '/example' and respond with 'Hello, world!'.

Passing Parameters in Routes

Often, you want to pass parameters to your routes. This can be useful for things like fetching a specific item from a database. In ExpressJS, you can pass parameters in routes using two methods: route parameters and query parameters.

Route Parameters

Route parameters are used to capture values ​​in the URL. They are defined in the route string using a colon and a name. For example, to capture a user ID in a route, you could define your route like this:

app.get('/users/:userId', function(req, res) {
  let userId = req.params.userId;
  // You can now use userId to fetch the user from the database
});

In this example, anything after '/users/' in the URL will be captured as 'userId'.

Query Parameters

Query parameters are another way to pass data to your routes. They are added to the URL after a question mark, and are separated by ampersands. For example, '/users?sort=desc'.

To access query parameters in ExpressJS, you use req.query. Here is an example:

app.get('/users', function(req, res) {
  let sort = req.query.sort;
  // You can now use sort to sort your users
});

In this example, 'sort' will be the value of the 'sort' query parameter in the URL.

Conclusion

Working with routes is a fundamental part of API development in ExpressJS. By understanding how to define routes and pass parameters, you can create more flexible and powerful APIs. Remember, practice is the key to becoming proficient in this aspect of ExpressJS, so keep experimenting and learning.

Now answer the exercise about the content:

_Which of the following statements is true about routes in ExpressJS?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Working with routes in ExpressJS: Working with dynamic routes

Next page of the Free Ebook:

49Working with routes in ExpressJS: Working with dynamic routes

4 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text