Node.js app.router和express.static的Expressjs顺序

Node.js app.router和express.static的Expressjs顺序,node.js,angularjs,express,Node.js,Angularjs,Express,要配置我的Expressjs应用程序,我有以下两行代码: 我已经读到建议将路由器置于静态之前,但当我这样做时,Angularjs应用程序会呈现一个空白页面。当视图处于显示顺序时,视图将正常渲染 为什么会这样?最好用一个例子来解释。比方说,您有一个en.json文件,还有一个路由: app.get('/en.json', function(req, res) { res.send('some json'); }); 如果你把 app.use(app.router); 以前 app.us

要配置我的Expressjs应用程序,我有以下两行代码:

我已经读到建议将路由器置于静态之前,但当我这样做时,Angularjs应用程序会呈现一个空白页面。当视图处于显示顺序时,视图将正常渲染


为什么会这样?

最好用一个例子来解释。比方说,您有一个
en.json
文件,还有一个路由:

app.get('/en.json', function(req, res) {
    res.send('some json');
});
如果你把

app.use(app.router);
以前

app.use(express.static(__dirname + '/public'));
路由器将优先于静态文件,用户将看到
一些json
,否则将提供
en.json
文件

所以,如果你没有这样的碰撞,你选择哪种顺序无关紧要

p.S.请注意,如果您使用的是Express 4,您可能会看到以下错误:

Error: 'app.router' is deprecated!
Please see the 3.x to 4.x migration guide for details on how to update your app.
no more app.use(app.router)

All routing methods will be added in the order in which they appear
在wiki页面@HectorCorrea已在评论中共享此错误的解释:

Error: 'app.router' is deprecated!
Please see the 3.x to 4.x migration guide for details on how to update your app.
no more app.use(app.router)

All routing methods will be added in the order in which they appear

希望这有帮助

您在哪里读到路由器应该先于静态路由器?这听起来不对。你想在路由器之前静态(正如你所拥有的)你有一个指向参考的链接吗?这里有一个例子-我在看最后一个答案,虽然它不是官方的正确答案,但似乎有很多支持。但我相信我也看到过其他例子,首先使用路由器,而不是明确引用这一点,我注意到,在查看Passportjs的示例文档时,路由器首先出现在我所看到的那些文档上。这看起来非常混乱,以至于Express 4.x将其删除:这提供了有关解决Express 4中“app.router已弃用”错误的信息-