How to intercept node.js express request

后端 未结 1 1476
孤独总比滥情好
孤独总比滥情好 2020-12-29 04:43

In express, I have defined some routes

app.post(\"/api/v1/client\", Client.create);
app.get(\"/api/v1/client\", Client.get);
...

I have def

相关标签:
1条回答
  • 2020-12-29 05:16

    You can do what you need in a couple of ways.

    This will place a middleware that will be used before hitting the router. Make sure the router is added with app.use() after. Middleware order is important.

    app.use(function(req, res, next) {
      // Put some preprocessing here.
      next();
    });
    app.use(app.router);
    

    You can also use a route middleware.

    var someFunction = function(req, res, next) {
      // Put the preprocessing here.
      next();
    };
    app.post("/api/v1/client", someFunction, Client.create);
    

    This will do a preprocessing step for that route.

    Note: Make sure your app.use() invokes are before your route definitions. Defining a route automatically adds app.router to the middleware chain, which may put it ahead of the user defined middleware.

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