express - Angular 2 通用 Heroku 部署

标签 express heroku angular seo

我一直在遵循使用 Angular 2 通用实现应用程序的教程。在heroku上我得到了

 at=error code=H10 desc="App crashed" method=GET path="/"

而 server.ts 是

// the polyfills must be the first thing imported in node.js
import 'angular2-universal/polyfills';

import * as path from 'path';
import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as cookieParser from 'cookie-parser';

// Angular 2
import { enableProdMode } from '@angular/core';
// Angular 2 Universal
import { expressEngine } from 'angular2-universal';

// enable prod for faster renders
enableProdMode();

const app = express();
const ROOT = path.join(path.resolve(__dirname, '..'));

// Express View
app.engine('.html', expressEngine);
app.set('views', __dirname);
app.set('view engine', 'html');

app.use(cookieParser('Angular 2 Universal'));
app.use(bodyParser.json());

// Serve static files
app.use('/assets', express.static(path.join(__dirname, 'assets'), {maxAge: 30}));
app.use(express.static(path.join(ROOT, 'dist/client'), {index: false}));


import { serverApi } from './backend/api';
// Our API for demos only
app.get('/data.json', serverApi);

import { ngApp } from './main.node';
// Routes with html5pushstate
// ensure routes match client-side-app
app.get('/', ngApp);
app.get('/about', ngApp);
app.get('/about/*', ngApp);
app.get('/home', ngApp);
app.get('/home/*', ngApp);
app.get('/ourwork', ngApp);
app.get('/ourwork/*', ngApp);
app.get('/services', ngApp);
app.get('/services/*', ngApp);
app.get('/portfolio', ngApp);
app.get('/portfolio/*', ngApp);
app.get('/contact', ngApp);
app.get('/contact/*', ngApp);

// use indexFile over ngApp only when there is too much load on the server
function indexFile(req, res) {
  // when there is too much load on the server just send
  // the index.html without prerendering for client-only
  res.sendFile('/index.html', {root: __dirname});
}

app.get('*', function(req, res) {
  res.setHeader('Content-Type', 'application/json');
  var pojo = { status: 404, message: 'No Content' };
  var json = JSON.stringify(pojo, null, 2);
  res.status(404).send(json);
});

// Server
app.listen(3000, () => {
  console.log('Listening on: http://localhost:3000');
});

我一直在四处寻找,但找不到这个问题的答案。任何帮助将不胜感激。

最佳答案

尝试使用:

// Server
app.listen((process.env.PORT || 3000), () => {
  console.log('Listening on port %d', this.address().port);
});

而不是

// Server
app.listen(3000, () => {
  console.log('Listening on: http://localhost:3000');
});

使用heroku给出的端口而不是硬编码3000。

关于express - Angular 2 通用 Heroku 部署,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38536600/

相关文章:

ruby-on-rails - 尝试登录和使用 heroku 时无法识别 ruby​​.exe

node.js - 解析错误: Expected another key-value pair

javascript - 在 for 循环内循环 - 嵌套数组

angular - 如何通过命令行在cypress.json文件中设置环境变量? (Angular CLI - nrwl nx)

javascript - 使用node和javascript将数据发送到服务器

sql-server - 从 Node js调用存储过程

javascript - Express - 如何在不使用查询字符串参数的情况下将数据传递到模型

Node.js 服务器等待的时间不会超过大约。 15 秒

node.js - 如何将 Heroku 生成的服务器端口应用到我的前端 Vue.js 应用程序?

javascript - 如何以 Angular 比较/合并两个数组