Middleware
Nest.js middleware is either a function or a class decorated with the @Injectable()
decorator that implements the NestMiddleware
interface. Middleware is called before route handlers. These functions have access to the request and response object, and they can makes changes to the request and response object.
One or more middleware functions can be configured for a route, and a middleware function can choose to pass the execution to the next middleware function on the stack or to end the request-response cycle.
If a middleware function does not end the request-response cycle, it must call next()
to pass control to the next middleware function or to the request handler if it is the last function on the stack. Failing to do so will leave the request hanging.
The AuthenticationMiddleware
in our blog application, for example, is responsible for authenticating a user that is accessing the blog.
import
{
MiddlewareFunction
,
HttpStatus
,
Injectable
,
NestMiddleware...