【问题标题】:Faster block all routes in Express更快地阻止 Express 中的所有路线
【发布时间】:2013-07-17 12:58:03
【问题描述】:
我试试
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(...);
这很好用。但我怀疑是否可以?
【问题讨论】:
标签:
javascript
node.js
express
routes
【解决方案1】:
app.use 更适合您希望在每个请求上处理的函数。它会稍微快一些,因为它避免了将路由与请求 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