How does Middleware work in Express?

The article has been translated. Link to original





This article is an adapted excerpt from the Express API Validation Essentials book . It will teach you a complete API validation strategy that you can start using in your Express applications today.





__________________





Express , " Express - , , middleware". , , , . , , :





  • middleware ?





  • next



    , , ?





  • middleware?





  • ?





middleware Express, , middleware .





(middleware). middleware Express , .





Middleware

Express Middleware - , . , , , . Express.





Express, - , , Middleware:





app.get("/user", function routeHandlerMiddleware(request, response, next) {
    // execute something
});
      
      



( 1.1)





Middleware . Express middleware , , API.





middleware, middleware . Express middleware. npm middleware Express.





Middleware

middleware:





/**
 * @param {Object} request - Express request object (commonly named `req`)
 * @param {Object} response - Express response object (commonly named `res`)
 * @param {Function} next - Express `next()` function
 */
function middlewareFunction(request, response, next) {
    // execute something
}
      
      



( 1.2)





: , req request, res response. , , , , , Express.





Express middleware, :





  • Express ( req



    ) - Node.js http.IncomingMessage.





  • Express ( res



    ) - Node.js http.ServerResponse.





  • Express next()



    - , next()



    , . , Express . middleware, , middleware, .





  • middleware return



    . , , Express.





Middleware

(middleware)





Middleware, Express, , "" ( Express ). , middleware ( 1.2).





middleware:





function plainMiddlewareFunction(request, response, next) {
    console.log(`The request method is ${request.method}`);

    /**
     * Ensure the next middleware function is called.
     */
    next();
}
      
      



( 1.3)





Middleware





  • middleware middleware , middleware , .. (error, request, response, next).





middleware :





function errorHandlingMiddlewareFunction(error, request, response, next) {
    console.log(error.message);

    /**
     * Ensure the next error handling middleware is called.
     */
    next(error);
}
      
      



( 1.4)





, next()



, .





function anotherMiddlewareFunction(request, response, next) {
    const error = new Error("Something is wrong");

    /**
     * This will cause Express to start executing error
     * handling middleware.
     */
    next(error);
}
      
      



( 1.5)





middleware

middleware . :

















, ( ) , , middleware , .





, middleware .









: , , .





app.get("/", someMiddleware, routeHandlerMiddleware, errorHandlerMiddleware);
      
      



( 1.6)









Express Router. middleware . , middleware , , .





import express from "express";

const router = express.Router();

router.use(someMiddleware);

router.post("/user", createUserRouteHandler);
router.get("/user/:user_id", getUserRouteHandler);
router.put("/user/:user_id", updateUserRouteHandler);
router.delete("/user/:user_id", deleteUserRouteHandler);

router.use(errorHandlerMiddleware);
      
      



( 1.7)









. , , .





app.use(someMiddleware);

// define routes

app.use(errorHandlerMiddleware);
      
      



( 1.8)





, app.use(someMiddleware)



, , someMiddleware



. , .





middleware , , . middleware, , , . middleware , . middleware. middleware .





middleware Express. middleware , Express.





middleware, Express :





  • : middleware





  • : middleware Express





__________________





"Express API Validation Essentials". API, Express- .





__________________





.





"Node.js Developer". , , Demo Day , 28 .





- DEMO DAY





.








All Articles