Deploy Angular 5 + Nodejs Express app to Heroku

浪尽此生 提交于 2019-12-03 04:57:15

Here's how I make my Angular app to deploy and work on Heroku:

  1. Your server.js should look something like this: https://hastebin.com/zavehahide.js
  2. In your package.json, move @angular/cli and @angular/compiler-cli from devDependencies to dependencies
  3. Also in your package.json, add postinstall: ng build --prod and start: node server.js to scripts

You should be good to go.

Usually when this error happens to me I have problems with node dependencies. Try to remove the node_modules folder, and the dist folder. From there spin everything up again, this mimics how heroku will build your project.

I suggest for more simplicity, if you want to deploy the backend on heroku as api and the he front end on github and activate cross origin resource sharing on browsing computer, I am actually building such app, that is my plan otherwise if you get any good news of this way your are doing update me

In your server.js you need to redirect your http call to the index.

app.route('/*', function(req,res) {
  res.redirect(__dirname + '/dist/index.html')
})

In the above it'll redirect any call to your index.html.

Correct your code in Server.js

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

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

app.listen(process.env.PORT||8080);


//Path Location Strategy
app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname+'/dist/index.html'));
});

console.log('Console Listening'); 

For future reference -

try running logs command before hitting the URL.

$ heroku logs

Then check the logs for further details.

You will face memory exceed issue if you are gonna build prod on heroku. So free type won’t work.

So try to build in local and serve your dist files using express.

Use same command in PROC file

Heroku local or Heroku local web : verify your changes in local

In answer how to debug further. Run your app, then straight away login to Heroku, go to your app, then in the 'More' dropdown, click 'View Logs'. Should help you out!

I believe there is a way to view the live logs through the terminal too so you can see exactly what is failing.

Try adding @angular-devkit/build-optimizer as a package in the package.json file.

"dependencies": {
    "@angular-devkit/build-optimizer": "^0.4.0",
    ...
}

This allows the post-install flag --aot to run. For some reason this package isn't built in anymore.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!