node.js – heroku中的Angular2路由

我在heroku中有一个angular2应用程序,我遇到路由器问题.在localhost中,一切都像魅力一样,但是当我部署到heroku并尝试通过任何非索引的路径访问时我得到了404错误,如果我去索引,那么导航到页面正常发生路由,除非我重新加载页面,然后我得到另一个404,这是heroku使用的package.json

“heroku-prebuild”:“npm install http-server -g”,

“heroku-postbuild”:“ng build –target = production –environment = prod&& rsync -a dist / *.”,

“开始”:“http-server dist /”,

我是否需要设置任何快速重写以在我的Procfile中使用? 最佳答案 它似乎是来自服务器的问题,angular知道路由但你的服务器不知道所有这些路径.简单的解决方案是将所有路径重定向到主index.html.像这样,

app.get('*', function (req, res) {
  res.sendfile('./dist/index.html'); // load our index.html file
});

这不会给出任何404错误,所有路径都将被重定向到主路径,即index.html,角度路由将与本地主机中的路径相同.

点赞