app with combined frontend and backend not deployi

2020-07-30 00:48发布

问题:

I have a angular frontend and nodejs backend app. The issue I am having is when I deploy to Heroku, the build and compile succeeds but I think only the backend is being run on heroku when I click open app. I looked at multiple tutorials and resources on how to deploy an angular and nodejs app on heroku and followed all the instructions but am unable to successfully get the full web app(both angular frontend and nodejs backend to Heroku.

the output I get when navigating to the app from the heroku url is:

{"message":"Invalid Token"}

my package.json is :

{
  "name": "eserver",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "main": "server.js",
    "heroku-postbuild": "ng build --prod",
    "preinstall": "npm install -g @angular/cli @angular/compiler-cli 
typescript",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^7.2.11",
    "@angular/cdk": "~7.3.3",
    "@angular/common": "^7.2.11",
    "@angular/compiler": "^7.2.11",
    "@angular/core": "^7.2.11",
    "@angular/flex-layout": "^7.0.0-beta.24",
    "@angular/forms": "^7.2.11",
    "@angular/http": "^7.2.11",
    "@angular/material": "^7.3.3",
    "@angular/platform-browser": "^7.2.11",
    "@angular/platform-browser-dynamic": "^7.2.11",
    "@angular/router": "^7.2.11",
    "@fortawesome/fontawesome-free": "^5.6.3",
    "@types/chart.js": "^2.7.42",
    "@types/web-bluetooth": "0.0.4",
    "angular-bootstrap-md": "^7.4.3",
    "body-parser": "^1.18.3",
    "bootstrap": "^4.3.1",
    "chart.js": "^2.5.0",
    "core-js": "^2.5.4",
    "cors": "^2.8.5",
    "dotenv": "^6.1.0",
    "express": "^4.16.4",
    "hammerjs": "^2.0.8",
    "material-design-lite": "^1.3.0",
    "ng-multiselect-dropdown": "^0.2.3",
    "popper.js": "^1.15.0",
    "pusher-js": "^4.4.0",
    "rxjs": "^6.4.0",
    "@angular-devkit/build-angular": "^0.6.8",
    "rxjs-compat": "^6.3.3",
    "@angular/compiler-cli": "^7.2.11",
    "@angular/cli": "~7.3.7",
    "tslib": "^1.9.0",
    "uuid": "^3.3.2",
    "zone.js": "^0.8.29",
    "bcryptjs": "^2.4.3",
    "express-jwt": "^5.3.1",
    "jsonwebtoken": "^8.2.2",
    "mongodb": "^3.0.10",
    "mongoose": "^5.1.4",
    "pusher": "^2.2.0",
    "rootpath": "^0.1.2",
    "typescript": "~3.2.4"
  },
  "devDependencies": {
    "@angular/language-service": "^7.2.11",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "angular-cli-ghpages": "^0.5.3",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "^5.4.1",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  }
}

my server.js is:

require('rootpath')();
const express = require('express');
const app = express();
const cors = require('cors');
const bodyParser = require('body-parser');
const jwt = require('./eserver-backend/_helpers/jwt');
const errorHandler = require('./eserver-backend/_helpers/error- handler');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());

// use JWT auth to secure the api
app.use(jwt());

// api routes
app.use('/users', require('./eserver- 
backend/users/users.controller'));
app.use('/categories', require('./eserver- 
backend/categories/categories.controller'));
app.use('/items', require('./eserver- 
backend/items/items.controller'));
app.use('/tableOrder', require('./eserver- 
backend/tableOrder/tableOrder.controller'));
app.use('/orders', require('./eserver- 
backend/order/order.controller'));
// global error handler
app.use(errorHandler);

// start server
const port = process.env.NODE_ENV === 'production' ? (process.env.PORT || 80) : 4000;
const server = app.listen(port, function () {
console.log('Server listening on port ' + port);
});

my procfile is:

web: node server.js

my file structure is: file structure

回答1:

Node.js app should serve the static files in dist folder which were built with ng build

Add the following code in your server.js file.

// Run the app by serving the static files
// in the dist directory
app.use(express.static(__dirname + '/dist'));

// For all GET requests, send back index.html
// so that PathLocationStrategy can be used
app.get('/*', function(req, res) {
  res.sendFile(path.join(__dirname + '/dist/index.html'));
});


回答2:

Adding this to the server.js fixed the issue app.use(express.static(__dirname + '/dist/angular-registration-login-example'));

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

  res.sendFile(path.join(__dirname+'/dist/angular-registration-login-example/index.html'));
});