express.js - single routing handler for multiple routes in a single line

后端 未结 5 856
悲&欢浪女
悲&欢浪女 2020-12-02 05:55

Is there a way to make this on a single function call?

var todo = function (req, res){};

app.get(\"/\", todo);
app.get(\"/blabla\", todo);
app.get(\"/blabla         


        
5条回答
  •  猫巷女王i
    2020-12-02 06:06

    Just to elaborate on Kevin's answer, this is from the 4.x docs:

    The path for which the middleware function is invoked; can be any of:

    • A string representing a path.
    • A path pattern.
    • A regular expression pattern to match paths.
    • An array of combinations of any of the above.

    They have some examples, including:

    This will match paths starting with /abcd, /xyza, /lmn, and /pqr:

    app.use(['/abcd', '/xyza', /\/lmn|\/pqr/], function (req, res, next) {
      next();
    });
    

提交回复
热议问题