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

don*_*don 2 google-app-engine node.js angular-universal

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

该应用程序通过运行在本地正常工作node server.js。

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

在package.jsonstart 命令中是"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');
});
Run Code Online (Sandbox Code Playgroud)

在 Google Cloud Console 日志查看器中,我成功地看到了“Listening on http://localhost:3000 ”。

don*_*don 9

找到了解决方案,如果有帮助,我会在这里发布。在port表达了请求(在GAE)引擎侦听必须被定义process.env.PORT

所以,而不是

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

肯定是:

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

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