代码之家  ›  专栏  ›  技术社区  ›  don

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

  •  0
  • don  · 技术社区  · 6 年前

    我正在尝试开发一个角度通用的应用程序,部署在googleappengine NodeJS标准环境中。

    node server.js

    Process terminated because the request deadline was exceeded. (Error code 123) .

    package.json 启动命令是 "node server.js" ,而在附录yaml我仅声明如下: runtime: nodejs8 .

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

    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');
    });
    

    在googlecloud控制台日志查看器中,我成功地看到了“监听” http://localhost:3000

    1 回复  |  直到 6 年前
        1
  •  8
  •   don    6 年前

    找到了解决方法,如果有用的话,我会把它贴在这里。这个 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}`);
    });