Creating Custom Middleware
With Express, you can create your own middleware. All you need to do is provide a function that accepts the Request object as the first parameter, the Response object as the second parameter, and next as the third parameter. The next parameter is a function passed by the middleware framework that points to the next middleware function to execute, so you must call next() prior to exiting your custom function, or the handler will never be called.
To illustrate how easy it is to implement your own custom middleware functionality in Express, Listing 19.10 implements a middleware function named queryRemover() that strips the query string off the URL before sending it on to the handler.
Notice that queryRemover() accepts ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access