Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS app not answering any request on GAE while working locally

I'm trying to develop an Angular Universal app to be deployed on a Google App Engine NodeJS standard environment.

The app works fine locally by running node server.js.

When deploying to GAE, however, the page keeps loading and it ultimately fails. In the Google Cloud Console Logs viewer I see this message: Process terminated because the request deadline was exceeded. (Error code 123).

In the package.json the start command is "node server.js", while in the app.yaml I declare only the following: runtime: nodejs8.

The server code (in TS, packed as JS with 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');
});

In the Google Cloud Console Logs viewer I successfully see "Listening on http://localhost:3000".

like image 938
don Avatar asked Dec 17 '25 12:12

don


1 Answers

Found the solution, in case it helps I'll post it here. The port on which express engine listens for requests (on GAE) must be defined with process.env.PORT

So, instead of

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

It must be:

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

app.listen(PORT, () => {
  console.log(`Listening on http://localhost:${PORT}`);
});
like image 174
don Avatar answered Dec 19 '25 05:12

don



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!