似乎我的nodejs应用程序永远不会在Heroku上部署。我的构建成功完成时显示以下消息:
[154] ./server.ts 2.1 kB {0} [built]
[238] ./src 160 bytes {0} [built]
[244] (webpack)/buildin/module.js 517 bytes {0} [built]
[259] ./dist/server/main.bundle.js 76.1 kB {0} [built]
+ 301 hidden modules
Node server listening on http://localhost:5000
我只是看到了Heroku的空白应用:
其他症状:
尽管我提供的是Procfile
,但事实如此:
web: npm start
查看我的package.json
:
{
"name": "vivonslagrasse-app",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "node dist/server.js",
"build": "ng build --prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"universal": "ng build --prod --aot && ng build --prod --app ssr --output-hashing=false && webpack --config webpack.server.config.js --progress --colors && node dist/server.js",
"postinstall": "npm run universal"
},
"private": true,
"dependencies": {
"@angular/animations": "^5.2.0",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/platform-server": "^5.2.9",
"@angular/router": "^5.2.0",
"@nguniversal/module-map-ngfactory-loader": "^5.0.0-beta.8",
"@ngx-translate/core": "^9.1.1",
"@ngx-translate/http-loader": "^2.0.1",
"@nicky-lenaers/ngx-scroll-to": "^0.6.0",
"ngx-cookie": "^2.0.1",
"core-js": "^2.4.1",
"foundation-sites": "^6.4.4-rc1",
"rxjs": "^5.5.6",
"ts-loader": "^3.5.0",
"xmlhttprequest": "^1.8.0",
"zone.js": "^0.8.19"
},
"devDependencies": {
"@angular/cli": "~1.7.0",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/jquery": "^3.3.1",
"@types/node": "~6.0.60",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "~2.5.3"
},
"engines": {
"node": "7.7.4",
"npm": "5.6.0"
}
}
请注意,我需要运行节点服务器,因为这是一个有角度的通用应用程序,初始页面的内容需要由服务器生成。
编辑:我添加了server.ts
文件(生成的server.js
太长了,无法在此处重现)
// These are important and needed before anything else
import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import { renderModuleFactory } from '@angular/platform-server';
import { enableProdMode } from '@angular/core';
import * as express from 'express';
import { join } from 'path';
import { readFileSync } from 'fs';
(global as any).XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();
// Express server
const app = express();
const PORT = process.env.PORT || 5000;
const DIST_FOLDER = join(process.cwd(), 'dist');
// Our index.html we'll use as our template
const template = readFileSync(join(DIST_FOLDER, 'browser', 'index.html')).toString();
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./dist/server/main.bundle');
const {provideModuleMap} = require('@nguniversal/module-map-ngfactory-loader');
app.engine('html', (_, options, callback) => {
renderModuleFactory(AppServerModuleNgFactory, {
// Our index.html
document: template,
url: options.req.url,
// DI so that we can get lazy-loading to work differently (since we need it to just instantly render it)
extraProviders: [
provideModuleMap(LAZY_MODULE_MAP)
]
}).then(html => {
callback(null, html);
});
});
app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));
// Server static files from /browser
app.get('*.*', express.static(join(DIST_FOLDER, 'browser')));
// All regular routes use the Universal engine
app.get('*', (req, res) => {
res.render(join(DIST_FOLDER, 'browser', 'index.html'), {req});
});
// Start up the Node server
app.listen(PORT, () => {
console.log(`Node server listening on http://localhost:${PORT}`);
});
毋庸置疑,此设置在我的开发机器上运行良好......
我们将非常感谢您的帮助。我已经花了2.5个小时。
答案 0 :(得分:0)
使用server.js中的process.env.port来监听
const port = process.env.port || 5000;
app.listen(port);