node.js 捕获除 /login 之外的所有路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19313016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Catch all route EXCEPT for /login
提问by clifford.duke
I am currently writing an API which will require a user to pass an authentication token in the header of each request. Now I know I can create a catchall route say
我目前正在编写一个 API,它将要求用户在每个请求的标头中传递一个身份验证令牌。现在我知道我可以创建一个包罗万象的路线说
app.get('/*', function(req,res){
});
but I was wondering how do I make it so that it excludes certain routes such as /loginor /?
但我想知道如何制作才能排除某些路线,例如/login或/?
回答by robertklep
I'm not sure what you want to happen when a user accesses /loginor /, but you can create separate routes for those; if you declare them before the catch-all, they get first dibs at handling the incoming requests:
我不确定当用户访问/login或时您希望发生什么/,但是您可以为这些创建单独的路由;如果你在包罗万象之前声明它们,它们在处理传入请求时会得到第一个 dib:
app.get('/login', function(req, res) {
...
});
app.get('/', function(req, res) {
...
});
app.get('*', function(req, res) {
...
});
回答by Leonid Beschastny
You can always place catch-all route after the ones you want to exclude (see robertklepanswer).
您始终可以在要排除的路线之后放置所有路线(请参阅robertklep回答)。
But sometimes you simply don't want to care about the order of your routes. In this case you still can do what you want:
但有时您根本不想关心路线的顺序。在这种情况下,您仍然可以做您想做的事:
app.get('*', function(req, res, next) {
if (req.url === '/' || req.url === '/login') return next();
...
});
回答by Seday Matzumiya
If you want to validate credentials or authenticity in every request you should use Express Routing feature "all", you can use it like this:
如果您想在每个请求中验证凭据或真实性,您应该使用“全部”快速路由功能,您可以像这样使用它:
app.all('/api/*', function(req, res, next){
console.log('General Validations');
next();
});
You could place it before any Routing stuff.
你可以把它放在任何路由的东西之前。
Note that in this case I used "/api/" as path, you can use "/" you it fits your needs.
请注意,在这种情况下,我使用“/api/ ”作为路径,您可以使用“/”来满足您的需求。
Hope it is not too late to help somebody here.
希望现在帮助这里的人还为时不晚。
回答by Jeremy Moritz
Another way to make a catch-all route handler is this:
制作一个包罗万象的路由处理程序的另一种方法是:
app.get('/login', function(req, res) {
//... login page
});
app.get('/', function(req, res) {
//...index page
});
app.get('/:pageCalled', function(req, res) {
console.log('retrieving page: ' + req.params.pageCalled);
//... mypage.html
});
This works exactly like robertklep's (accepted) answer, but it gives you more information about what the user actually requested. You now have a slug req.params.pageCalledto represent whatever page is being requested and can direct the user to the appropriate page if you have several different ones.
这与 robertklep 的(已接受)答案完全一样,但它为您提供了有关用户实际请求内容的更多信息。您现在有一个 slugreq.params.pageCalled来表示正在请求的任何页面,并且如果您有几个不同的页面,可以将用户引导到适当的页面。
One gotchya to watch out for (thx @agmin) with this approach, /:pageCalledwill only catch routes with a single /, so you will not get /route/1, etc. Use additional slugs like /:pageCalled/:subPageCalledfor more pages (thx @softcode)
一个gotchya要提防(THX @agmin)这种方法,/:pageCalled只赶上与航线单/,这样你就不会得到/route/1,等等。使用额外的蛞蝓一样/:pageCalled/:subPageCalled更多页面(THX @softcode)

