【问题标题】:Error deploying Angular project on heroku在 heroku 上部署 Angular 项目时出错
【发布时间】:2021-01-19 19:27:29
【问题描述】:

我已将我的 Angular 项目部署到 heroku。它显示错误未找到。我尝试在节点 server.js 中运行相同的项目,同样的错误是找不到一些文件。 我试过这个https://itnext.io/how-to-deploy-angular-application-to-heroku-1d56e09c5147。 但我认为 server.js 文件中有一些错误。 服务器.js

const express = require('express');
const app = express();
const path = require('path');
const port = process.env.PORT || 4200;
const server = require('http').Server(app);


// Serve only the static files form the angularapp directory
app.use(express.static(__dirname + '/RouterApp'));

app.get('/*', function(req,res) {

res.sendFile(path.join(__dirname+'/RouterApp/index.html'));
});

// Start the app by listening on the default Heroku port


server.listen(port, function () {
    console.log("App running on port " + port);
})

当我尝试将路径设置为/src/index.html 时,正在加载索引页面,但未加载 Angular 组件。 这是我的目录结构

>>RouterApp
  >>...
  >>server.js
  >>src
    >>index.html
    >>components
  >>...

使用node server.js 运行时遇到的错误 ENOENT:没有这样的文件或目录,stat '/data/data/com.termux/files/home/RouterApp/RouterApp/index.html'

当我尝试在localhost:4200 上运行程序时,使用ng serve 运行得非常好。 这是 GitHub 存储库https://github.com/Pradeep2898/QuoraClone

【问题讨论】:

    标签: node.js angular heroku


    【解决方案1】:

    好的,我明白了。我的项目没有在部署时创建 dist 文件夹。我试过这个步骤

    cd RouterApp
    ng build  //this will creat a dist folder
    // now run the node server.js
    node server.js
    >>App running on port 4200
    // Open the app locally on port 4200, https: localhost:4200
    // It will work. Push the changes to git repository.
    // It worked.
    

    https://quora-ui-clone.herokuapp.com/

    尝试安装 npm install http-server -g 然后运行http-server/dist

    How to run the Dist Folder on Local Machine in Angular 6+?

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 2022-11-08
      相关资源
      最近更新 更多