Calling already defined routes in other routes in Express NodeJS

后端 未结 5 1503
傲寒
傲寒 2020-12-29 20:11

I am writing a web app in node.js using Express. I have defined a route as follows:

app.get(\"/firstService/:query\", function(req,res){
    //trivial exampl         


        
5条回答
  •  隐瞒了意图╮
    2020-12-29 20:49

    If you have a lot of middleware on your route, you can benefit from spreading:

    const router = express.Router();
    
    const myMiddleware = [
        authenticationMiddleware(),
        validityCheckMiddleware(),
        myActualRequestHandler
    ];
    
    router.get( "/foo", ...myMiddleware );
    router.get( "/v1/foo", ...myMiddleware );
    

提交回复
热议问题