Search code examples
node.jsangularexpressheroku

Problem with deploying Angular on Heroku by creating server.js


I have some problems with deploying app on Heroku.

First of all: I don't understand why I have some memory restriction in CSS files?

I solved that problem later when I had cleaned all listed CSS files (Screen below)

This errors that I fixed later

Then I've got different problem with it. I saw several examples about how to deploy Angular app on Heroku and there were clearly identical "server.js", but I still don't understand why do we need to use "./dist" and with all examples that I saw - I'm totally confused how we can find index.html?

This is an example of how server.js should look like.

const express = require('express');

const app = express();

app.use(express.static('./dist/angular-heroku'));

app.get('/*', (req, res) =>
    res.sendFile('index.html', {root: 'dist/angular-heroku/'}),
);

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

And this is my implementation:

const express = require('express');

const app = express();

app.use(express.static('./dist/Web-App'));

app.get('/*', (req, res) =>
  res.sendFile('index.html', {root: 'dist/Web-App/'}),
);

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

So Web-App is my folder where I hold my Angular project, but I'm confused: I've got "index.html" inside "Web-App/src/index.html. How can it find index.html without including at least "/src" direction?

Here is my package.json:

{
  "name": "Web-App",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "node server.js",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "heroku-postbuild": "ng build --prod",
    "postinstall": "ng build"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~11.1.2",
    "@angular/cli": "^11.2.1",
    "@angular/common": "~11.1.2",
    "@angular/compiler": "~11.1.2",
    "@angular/compiler-cli": "^11.1.2",
    "@angular/core": "~11.1.2",
    "@angular/forms": "~11.1.2",
    "@angular/platform-browser": "~11.1.2",
    "@angular/platform-browser-dynamic": "~11.1.2",
    "@angular/router": "~11.1.2",
    "bootstrap": "^4.6.0",
    "bootstrap-icons": "^1.3.0",
    "express": "^4.17.1",
    "jquery": "^3.5.1",
    "path": "^0.12.7",
    "popper.js": "^1.16.1",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "typescript": "~4.1.2",
    "zone.js": "~0.11.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1101.4",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "ejs": "^3.1.6",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.2.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0"
  },
  "engines": {
    "node": "10.19.0",
    "npm": "6.14.11"
  }
}

When I deploy my app it just display "Error" as a title and only this:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Not Found</pre>
</body>
</html>

Solution

  • The problem was with different name of my app, that was in /dist/.

    Name was totally different from my app-name.

    I just decided to check if everything is all right when I generate "dist" direction.

    So I went for "ng build" and found out that /dist direction holds different name.

    Also to solve the problem with size you need to change it in "angular.json" budgets

    budgets