You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You might've heard about ExpressJS: probably the most popular Node.js framework. You might've also heard the word middleware a whole lot. But what is middleware?
Your standard Express-application usually consists of a bunch of routes (also calles endpoints), as well as a number of handlers for each of these routes, which are executed consecutively. These handlers are called middleware. Now let's go into some detail.
Middleware functions (except for error-handling middleware, we'll cover them later) have access to the request object (req), response object (res), as well as the next middleware function.
Middleware functions can do several things:
Change the req and res objects
End the request-response cycle (e.g. by calling res.end() or res.send())
Call the next middleware function (if the current middleware function doesn't end the request-response cycle, it should call next(), or the request will be left hanging)
Execute code
Here's an example: let's have a look at a simple "Hello world" app.
The '/' route has a single middleware function that ends the request-response cycle by responding with "Hello world!".
And here's a simple logger function:
Add it to the whole app (function is executed for every route, that is defined afterapp.use):
app.use(logger);
There are several types of middleware:
Binding a function to an instance of the app object by using the app.use() or app.METHOD() makes it application-level middleware. It's important to note here that the order in which you add application-level middleware to your app matters: as with all types of middleware, application-level middleware will be executed (or checked for, in case of routes) in order it was defined.
Router-level middleware is similar to application-level, except it is bound to an instance of express.Router()
Error-handling middleware. Passing anything to the next() function (except the string 'route') will skip any remaining non-error-handling middleware an pass control to the first error-handling middleware. Unlike other middleware function, error-handling middleware accept four arguments: err, req, res, next:
Third-party middleware is used to extend the functionality of Express apps (e.g. cookie-parser, body-parser).
To sum up, middleware functions make Express apps customizable and easy to build by allowing to add stuff like error-handlers, validation, loggers and application logic to both single and multiple routes, as well as increase functionality by adding libraries as midleware functions.
The text was updated successfully, but these errors were encountered:
@Czech-nut The tool found a lot of matches between the article and the official Express documentation and other articles about Express, most of them being terminology (e.g. application-level middleware, end the request-response cycle) and common code. Considering that the main goal of the article is to concisely convey everything about middleware from said documentation I will be unable to rewrite it without either avoiding the essential terms and code examples (making the article pointless) or completely changing the topic
You might've heard about ExpressJS: probably the most popular Node.js framework. You might've also heard the word middleware a whole lot. But what is middleware?
Your standard Express-application usually consists of a bunch of routes (also calles endpoints), as well as a number of handlers for each of these routes, which are executed consecutively. These handlers are called middleware. Now let's go into some detail.
Middleware functions (except for error-handling middleware, we'll cover them later) have access to the request object (
req
), response object (res
), as well as thenext
middleware function.Middleware functions can do several things:
req
andres
objectsres.end()
orres.send()
)next
middleware function (if the current middleware function doesn't end the request-response cycle, it should callnext()
, or the request will be left hanging)Here's an example: let's have a look at a simple "Hello world" app.
The '/' route has a single middleware function that ends the request-response cycle by responding with "Hello world!".
And here's a simple logger function:
Now, there are multiple ways to add this function as a middleware function. You can:
app.use
):There are several types of middleware:
app.use()
orapp.METHOD()
makes it application-level middleware. It's important to note here that the order in which you add application-level middleware to your app matters: as with all types of middleware, application-level middleware will be executed (or checked for, in case of routes) in order it was defined.express.Router()
next()
function (except the string 'route') will skip any remaining non-error-handling middleware an pass control to the first error-handling middleware. Unlike other middleware function, error-handling middleware accept four arguments:err
,req
,res
,next
:express.static
, which is used to serve static content:cookie-parser
,body-parser
).To sum up, middleware functions make Express apps customizable and easy to build by allowing to add stuff like error-handlers, validation, loggers and application logic to both single and multiple routes, as well as increase functionality by adding libraries as midleware functions.
The text was updated successfully, but these errors were encountered: