Home > Blockchain >  GCP AppEngine nodejs The server encountered an error and could not complete your request
GCP AppEngine nodejs The server encountered an error and could not complete your request

Time:04-20

I have a NodeJs backend hosted on AppEngine, when I request some url locally with Postman it works well but when I request them in production env (AppEngine) it returns me the following html error after a long time (timeout):

<html>

<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>500 Server Error</title>
</head>

<body text=#000000 bgcolor=#ffffff>
    <h1>Error: Server Error</h1>
    <h2>The server encountered an error and could not complete your request.<p>Please try again in 30 seconds.</h2>
    <h2></h2>
</body>

</html>

I've also tested with a minimum working project, just an index.js with the following code:

const http = require('http');

const port = 8000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, () => {
  console.log(`Server running at port ${port}`);
});

and it does the same.

My app,yaml is this one:

runtime: nodejs16
service: test
instance_class: F2
env: standard

I went to see the logs in AppEngine logs and the error is this one:

Process terminated because the request deadline was exceeded. Please ensure that your HTTP server is listening for requests on 0.0.0.0 and on the port defined by the PORT environment variable. (Error code 123)

I've tried to modify the app.yaml, to delete my service and recreate it, to test with a small project and the result is still the same ...

I don't understand where the issue is. Does someone know where the issue is ?

Thank you !

CodePudding user response:

I'm quite new to gcp AppEngine, but I think this article can help. From the documentation:

Importantly, on the last few lines, the code has the server listen to the port specified by the process.env.PORT variable. This is an environment variable set by the App Engine runtime - if your server does not listen to this port, it will not be able to receive requests.

so try to add this

const port = parseInt(process.env.PORT) || 8000;
  • Related