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

后端 未结 23 1410
别跟我提以往
别跟我提以往 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:22

    app.use is a function requires middleware. For example:

     app.use('/user/:id', function (req, res, next) {
           console.log('Request Type:', req.method);
            next();
         });
    

    This example shows the middleware function installed in the /user/:id path. This function is executed for any type of HTTP request in the /user/:id path.

    It is similar to the REST Web Server, just use different /xx to represent different actions.

提交回复
热议问题