【问题标题】:routes in angular and nodejs deployed app not working角度和nodejs部署的应用程序中的路由不起作用
【发布时间】:2021-06-24 20:52:15
【问题描述】:

我已经在 heroku 上部署了一个 nodejs 和 angular 应用程序,但部署后的路由无法正常工作。 这是我的网站 - https://murti123.herokuapp.com 有路线 - enter link description here

但是对于路由,它会给出can't get / 错误。 不知道怎么解决

Here is My project Structureenter code here

【问题讨论】:

    标签: node.js angular heroku


    【解决方案1】:

    你需要resolve你的前端应用路径

    所以假设在/public 文件夹中你有来自构建的前端应用程序的 dist 文件

    app.use(express.static(__dirname + "/public"));
    

    在这里您可以为任何路线解析 index.html

    app.get("*", function (req, res) {
      //path is required at the top of the file ofcourse
      //const path = require("path"); 
      res.status(200).sendFile(path.resolve(__dirname + "/public/index.html"));
    });
    

    【讨论】:

    • 如果这回答了您的问题,您可以将其标记为正确答案:)
    • 这个路径有依赖吗?
    • '这条路径是否有依赖关系?' -- 是 Node.js 提供的内置模块
    猜你喜欢
    • 2018-10-04
    • 2016-06-25
    • 2016-05-17
    • 1970-01-01
    • 2019-02-02
    • 2019-10-15
    • 1970-01-01
    • 2017-07-24
    • 2017-06-22
    相关资源
    最近更新 更多