7.4. Working with routes in ExpressJS: Using middleware in routes

Página 50

One of the most important aspects of creating APIs is defining and manipulating routes. Routes are the paths that users or clients of an API take to access different resources or functionality. In NodeJS, we use ExpressJS, a minimalist and flexible framework, to handle routes efficiently. One of the most powerful features of ExpressJS is the ability to use middleware in routes. Let's explore this functionality in detail.

What are Middlewares?

In simple terms, middlewares 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. They are used to modify req and res, or to terminate the request/response cycle. If a middleware does not finish the loop, it must call next() to pass control to the next middleware.

Using Middlewares in Routes

Middleware in ExpressJS can be applied globally or to specific routes. To apply middleware to a specific route, you simply pass it as an additional argument to the route handler. For example:

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

In this case, middlewareFunction is a function that will be executed before the route handler. It can modify the req or res object, or even terminate the request and send a response.

Middleware Examples

There are many practical uses for middleware in routes. Here are some examples:

Authentication

An authentication middleware can verify that a user is authenticated before allowing them to access a route. If the user is not authenticated, the middleware can terminate the request and send an error response. If the user is authenticated, the middleware can add user information to the req object so the route handler can use it.

Data validation

A data validation middleware can verify that the data sent in a POST or PUT request is valid. If the data is not valid, the middleware can terminate the request and send an error response. If the data is valid, the middleware can pass control to the next middleware or route handler.

Log

A logging middleware can log details about each request to a file or database. This can be useful for debugging or keeping a record of all requests coming in to the server.

Conclusion

Middlewares provide a powerful way to add functionality to your routes in ExpressJS. They allow you to reuse logic across multiple routes while keeping your DRY (Don't Repeat Yourself) code. Additionally, they can help improve the security of your API by ensuring that only authenticated users can access certain routes, and that data sent to your API is always valid.

I hope this chapter has given you a good understanding of how to work with routes in ExpressJS and how to use middleware in routes. In the next chapter, we'll dive deeper into ExpressJS and explore some of its other powerful features.

Now answer the exercise about the content:

What are Middlewares in the context of ExpressJS and how are they used?

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

You missed! Try again.

Next page of the Free Ebook:

517.5. Working with Routes in ExpressJS: Working with Authentication Routes

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