简体   繁体   English

快速阻止Express中的所有路线

[英]Faster block all routes in Express

I try 我试试

var _LOCK_ = true; // or load it from config

app.all('*', function(req,res,next){
   if(_LOCK_) return res.send(401);
   next();
});

// this place other routes
app.get(...);
app.post(...);

This works well. 这很好用。 But I doubt whether all right? 但我怀疑是否可以吗?

app.use is more appropriate for a function that you want processed on every request. app.use更适合您希望在每个请求上处理的函数。 It will be slightly faster since it avoids having to match the route against the request URL. 它会稍微快一点,因为它避免了必须匹配请求URL的路由。

var _LOCK_ = true; // or load it from config

app.use(function(req,res,next){
   if(_LOCK_) return res.send(401);
   next();
});
app.use(app.router); // your middleware function must be above the router

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM