Angular – Node – 可以告诉节点从哪个路由请求块

是否有可能告诉我的节点应用程序req在:

 app.get('/*', function(req, res) { res.sendFile(__dirname + '/site/index.html') }); 

我的angular路线从哪个方块出来(即我希望能够识别来自otherwise方块的什么);

 .when('/user', {...} ) .when('/login', {...} ) .otherwise( {...} ) 

我想要的原因是因为我想要做这样的事情:

 app.get('/*', function(req, res) { if (req is from otherwise block) { res.status(400).sendFile(__dirname + '/site/index.html') } else { res.sendFile(__dirname + '/site/index.html') } }); 

我唯一能想到的是创build一个所有允许的路由的数组,但是我不喜欢重复:p

由于Angular和Node之间的唯一联系就是HTTP请求,所以你只需要检索URL并分析它来自哪个路由。

我会在Node中定义你知道的路线,然后有一个404错误的回退:

 app.get(['/login', '/user'], function(req, res) { res.sendFile(__dirname + '/site/index.html') }); app.get('*', function(req, res) { res.status(404).sendFile(__dirname + '/site/404.html') });