节点js 404和angular url刷新冲突

Bab*_*tha 3 javascript nodes node.js angularjs angularjs-routing

我正在制作Nodejs和angular js应用程序.当有人在网址上输入并输入网页时,我不得不加载页面.它工作正常,直到我还制作了一个脚本重定向到404错误页面.现在的问题是两个脚本只有一个标准有效.当404重定向工作时,我无法访问在浏览器上键入url的页面,当它工作时,404页面被重定向到索引.HTML.

这是我使用的两个脚本.

app.use(function(req, res) {
    res.render('404', {layout: false, title: '404: File Not Found'});
});

app.use('/*', function (req, res) {
   res.sendFile(__dirname + '/public/index.html');
});
Run Code Online (Sandbox Code Playgroud)

我在这做错了吗?除此之外,当我从导航中点击它时,我也有我的一般路线在角应用程序中工作正常.

Man*_*wal 5

将404处理程序代码放在最后(必须是最后一条路径)

像这样使用你的代码:

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

app.use(function(req, res) { //put this at end
    res.status(404);//add this line for setting 404 status
    res.render('404', {layout: false, title: '404: File Not Found'});
});
Run Code Online (Sandbox Code Playgroud)