NodeJS应用程序在本地工作时未回答GAE上的任何请求

时间:2018-07-08 17:44:12

标签: node.js google-app-engine angular-universal

我正在尝试开发Angular Universal应用程序,以将其部署在Google App Engine NodeJS标准环境中。

通过运行node server.js,该应用可以在本地正常运行。

但是,当部署到GAE时,页面会继续加载,最终会失败。在Google Cloud Console日志查看器中,我看到以下消息:Process terminated because the request deadline was exceeded. (Error code 123)

package.json中,启动命令是"node server.js",而在app.yaml中,我仅声明以下内容:runtime: nodejs8

服务器代码(在TS中,与WebPack打包为JS):

import { enableProdMode } from '@angular/core';
import * as ngUniversal from '@nguniversal/express-engine';
import * as compression from 'compression';
import * as express from 'express';
import * as path from 'path';
import * as detector from 'spider-detector';
import * as appServer from '../dist/server/main.js';
require('zone.js/dist/zone-node');

const ROOT = path.resolve();

enableProdMode();

const app = express();

// Server-side rendering
function angularRouter(req, res): void {
  res.render('index', { req, res });
}

// Enable compression
app.use(compression());

// Check if the user is a bot
app.use(detector.middleware());

// Root route before static files, or it will serve a static index.html, without pre-rendering
app.get('/', angularRouter);

// Serve the static files generated by the CLI (index.html, CSS? JS, assets...)
app.use(express.static('client'));

// Configure Angular Express engine
app.engine('html', ngUniversal.ngExpressEngine({
  bootstrap: appServer.AppServerModuleNgFactory
}));
app.set('view engine', 'html');
app.set('views', path.join(ROOT, 'client'));

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

在Google Cloud Console日志查看器中,我成功看到“在http://localhost:3000上收听”。

1 个答案:

答案 0 :(得分:2)

找到了解决方案,以防万一,我将其张贴在这里。表达引擎在GAE上侦听请求的port必须用process.env.PORT

定义

所以,而不是

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

它必须是:

const PORT = process.env.PORT || 3000;

app.listen(PORT, () => {
  console.log(`Listening on http://localhost:${PORT}`);
});