简体   繁体   English

将 Angular 应用程序部署到 heroku 时出错

[英]Error while deploying an angular app to heroku

I am trying to deploy an angular app "test-app" to heroku through github and我正在尝试通过 github 和
everything works just fine but I cant setup the express routing function.一切正常,但我无法设置快速路由功能。

I tried various paths but heroku just put this error.我尝试了各种路径,但 heroku 只是把这个错误。

Error: ENOENT: no such file or directory, stat '/app/test-app/index.html'错误:ENOENT:没有这样的文件或目录,stat '/app/test-app/index.html'

and below is my "server.js" file下面是我的“server.js”文件

    const express = require("express");
    const path = require("path");
    const app = express();

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

    app.get("/*", function(req, res) {
    res.sendFile(path.join(__dirname, "/dist/test-app/index.html"));
    });

    app.listen(process.env.PORT || 8080, () => {
    console.log("Connected to the server");
    });

I think I messed up something in the "path join" section.Can someone please help我想我在“路径连接”部分搞砸了一些东西。有人可以帮忙吗
me out here ?我在这里?

Did you build your angular application with ng build?.您是否使用 ng build 构建了 Angular 应用程序? If yes, then did you put the output of the build into the dist folder?如果是,那么您是否将构建的输出放入 dist 文件夹中?

I would suggest the following: 1. Seperate your express server and you angular app.我建议如下: 1. 分离你的快递服务器和你的角度应用程序。 2. Build your angular app and copy the output(only the html and js files not the folder with the project name) into a directory in your express app(lets say you name it dist as well). 2. 构建您的 angular 应用程序并将输出(仅 html 和 js 文件,而不是具有项目名称的文件夹)复制到您的 express 应用程序中的目录中(假设您也将其命名为 dist)。 3. In you express app you only need this: app.use(express.static(__dirname + "/dist")); 3. 在你的快递应用中你只需要这个: app.use(express.static(__dirname + "/dist")); and not并不是

app.get("/*", function(req, res) {
res.sendFile(path.join(__dirname, "/dist/test-app/index.html"));
});. 

You can make it work just with part 3 even if you put your express and angular app in the same workspace, but its better to seperate the two in the future as they are two different entities that interact with each other but may need a lot of development seperately.即使您将 express 和 angular 应用程序放在同一个工作区中,您也可以使其仅使用第 3 部分,但是将来最好将两者分开,因为它们是相互交互的两个不同实体,但可能需要很多分别开发。

Edit: log your __dirname and see what it shows.编辑:记录您的 __dirname 并查看它显示的内容。 Most of the times app.use(express.static("dist"));大多数时候app.use(express.static("dist")); is enough.足够。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM