api - How to intercept node.js express request -
in express, have defined routes
app.post("/api/v1/client", client.create); app.get("/api/v1/client", client.get); ...
i have defined how handle requests inside client controller. there way can pre-processing requests, before handling them in controller? want check if api caller authorized access route, using notion of access levels. advice appreciated.
you can need in couple of ways.
this place middleware used before hitting router. make sure router added app.use()
after. middleware order important.
app.use(function(req, res, next) { // put preprocessing here. next(); }); app.use(app.router);
you can use route middleware.
var somefunction = function(req, res, next) { // put preprocessing here. next(); }; app.post("/api/v1/client", somefunction, client.create);
this preprocessing step route.
note: make sure app.use()
invokes before route definitions. defining route automatically adds app.router middleware chain, may put ahead of user defined middleware.
Comments
Post a Comment