NodeJS / Express: what is “app.use”?

后端 未结 23 1319
别跟我提以往
别跟我提以往 2020-11-29 14:48

In the docs for the NodeJS express module, the example code has app.use(...).

What is the use function and where is it defined?

相关标签:
23条回答
  • 2020-11-29 15:07

    As the name suggests, it acts as a middleware in your routing.

    Let's say for any single route, you want to call multiple url or perform multiple functions internally before sending the response. you can use this middleware and pass your route and perform all internal operations.

    syntax:
    app.use( function(req, res, next) {
      // code
     next();
    })
    

    next is optional, you can use to pass the result using this parameter to the next function.

    0 讨论(0)
  • 2020-11-29 15:10

    It enables you to use any middleware (read more) like body_parser,CORS etc. Middleware can make changes to request and response objects. It can also execute a piece of code.

    0 讨论(0)
  • 2020-11-29 15:12

    app.use() is an middleware method.

    Middleware method is like a interceptor in java, this method always executes for all request.

    Purpose and use of middleware:-

    1. To check if session expired or not
    2. for user authentication and authorization
    3. check for cookie (expire date)
    4. parse data before response
    0 讨论(0)
  • 2020-11-29 15:13
    app.use(function middleware1(req, res, next){
       // middleware1 logic
    }, function middleware1(req, res, next){
       // middleware2 logic
    }, ... middlewareN);
    

    app.use is a way to register middleware or chain of middlewares (or multiple middlewares) before executing any end route logic or intermediary route logic depending upon order of middleware registration sequence.

    Middleware: forms chain of functions/middleware-functions with 3 parameters req, res, and next. next is callback which refer to next middleware-function in chain and in case of last middleware-function of chain next points to first-middleware-function of next registered middlerare-chain.

    0 讨论(0)
  • 2020-11-29 15:14

    You can also create your own middleware function like

    app.use( function(req, res, next) {
      // your code 
      next();
    })
    

    It contains three parameters req, res, next
    You can also use it for authentication and validation of input params to keep your controller clean.

    next() is used for go to next middleware or route.
    You can send the response from middleware

    0 讨论(0)
  • 2020-11-29 15:15

    app.use() handles all the middleware functions.
    What is middleware?
    Middlewares are the functions which work like a door between two all the routes.

    For instance:

    app.use((req, res, next) => {
        console.log("middleware ran");
        next();
    });
    
    app.get("/", (req, res) => {
        console.log("Home route");
    });
    

    When you visit / route in your console the two message will be printed. The first message will be from middleware function. If there is no next() function passed then only middleware function runs and other routes are blocked.

    0 讨论(0)
提交回复
热议问题