`app.get(“/”,func1,func2);`和app.get(“/”,func1)一样。 app.get(“/”,func2);`?
我正在尝试在express.js中为我的web应用程序创build一个路由系统,我需要知道是否需要使用app.get / post / put / delete.apply以编程方式为一条路线设置多个function。
那么
app.get("/", function(req, res, next) { code(); next(); }); app.get("/", function(req, res, next) { finish(); });
一样
app.get("/", function(req, res, next) { code(); next(); }, function(req, res, next) { finish(); });
?
是的,这几乎是一样的。
如果可能,可以使用app.use
将“设置”function提升为适当的中间件:
app.use(function(req, res, next) { code(); next(); });
但只有在需要为所有路线运行时才有用。
或者,如果您想重复使用某些路线,可以这样做:
var MyMiddleware = function(req, res, next) { code(); next(); }); app.get("/", MyMiddleware, function(req, res) { finish(); });