Page MenuHomePhabricator

Receiving 502 Bad Gateway with nodejs webservice
Closed, ResolvedPublic

Description

I'm having trouble getting a nodejs app up and running. At present, with the webservice running, https://tools.wmflabs.org/translation-server reads "502 Bad Gateway".

I've checked out the script from https://github.com/zotero/translation-server, and have been able to get it running locally using npm start (though sometimes it complains that its default port, 1969, is not available).

I've modified the script to run as a webservice by replacing
var port = config.get('port'); with var port = parseInt(process.env.PORT, 10);, per https://wikitech.wikimedia.org/wiki/Help:Toolforge/Web

When I run npm start now, I receive the error Port should be > 0 and < 65536. Received NaN.; presumably process.env.PORT is not defined in a shell context.

The other thing I've noticed is that server.js sits in www/js/src rather than www/js. I've tried copying the file to www/js but still get the 502 Bad Gateway message, which makes it tricky to debug what's going on. But this said, the package.json does point to src/server.js as the default script.

If anyone has any thoughts as to what I might try or how I might debug them, I'd be very glad to hear them!

Related Objects

StatusSubtypeAssignedTask
ResolvedNone
ResolvedNone

Event Timeline

Smith609 renamed this task from Accessing PORT for nodejs webservice to Receiving 502 Bad Gateway with nodejs webservice.Sep 26 2018, 2:32 PM
Smith609 updated the task description. (Show Details)
root@tools-bastion-05:~# sudo -Hu tools.translation-server kubectl get po -o wide
NAME                                  READY     STATUS             RESTARTS   AGE       IP               NODE
interactive                           1/1       Running            0          8h        192.168.168.14   tools-worker-1001.tools.eqiad.wmflabs
translation-server-1233425354-b5dl8   0/1       CrashLoopBackOff   7          11m       192.168.234.16   tools-worker-1003.tools.eqiad.wmflabs
root@tools-bastion-05:~# sudo -Hu tools.translation-server kubectl logs translation-server-1233425354-b5dl8
(node:1) fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module, please update it to a more recent version.

> translation-server@0.0.1 start /data/project/translation-server/www/js
> node src/server.js

/data/project/translation-server/www/js/node_modules/koa-bodyparser/index.js:72
  return async function bodyParser(ctx, next) {
               ^^^^^^^^
SyntaxError: Unexpected token function
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/data/project/translation-server/www/js/src/server.js:30:20)

npm ERR! translation-server@0.0.1 start: `node src/server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the translation-server@0.0.1 start script.
npm ERR! This is most likely a problem with the translation-server package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node src/server.js
npm ERR! You can get their info via:
npm ERR!     npm owner ls translation-server
npm ERR! There is likely additional logging output above.
npm ERR! System Linux 4.9.0-0.bpo.6-amd64
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "start"
npm ERR! cwd /data/project/translation-server/www/js
npm ERR! node -v v6.11.0
npm ERR! npm -v 1.4.21
npm ERR! code ELIFECYCLE
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
 
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /data/project/translation-server/www/js/npm-debug.log
npm ERR! not ok code 0

FWIW,

tools.zhuyifei1999-test@interactive:~$ node --version
v6.11.0

Sounds like you've found the problem! Node v8+ is required for the script.

When I run a webservice shell, node -v renders v11.0.0-pre.
This is because I add a directory containing node v11 to the $PATH using an export command in ~/.profile.

Any ideas why the .profile file is not being executed when I run webservice start?

Any ideas why the .profile file is not being executed when I run webservice start?

Because it is a direct execv without going through the shell.

Thanks for the link. It looks like jswebservice.py doesn't refer to the $PATH variable.

Is there an alternative way to direct the script to a current version of npm?

  • Would I be able to install or link to a current version of npm in /usr/local/bin, as the script searches for?
  • Or is it possible to edit jswebservice.py so that it looks for binaries in ~/local/bin before resorting to /usr/local/bin?

Could you change it within npm start?

  • Would I be able to install or link to a current version of npm in /usr/local/bin, as the script searches for?

No, the directory is root-owned.

  • Or is it possible to edit jswebservice.py so that it looks for binaries in ~/local/bin before resorting to /usr/local/bin?

Feel free to submit a patch, but I don't think that will help. The concern is with the version of node, not npm.

Could you change it within npm start?

Great suggestion, thanks! I've edited package.json to replace

"start": "node src/server.js",

with

"start": "~/local/bin/node src/server.js",

Instead of the 502 bad gateway, I'm now getting the message "Not Found"... which is definitely progress and hopefully means I just need to work out how to configure my script, rather than the server!

Thanks very much for your help on this. Much appreciated; I've been completely in the dark aside from your guidance!

Instead of the 502 bad gateway, I'm now getting the message "Not Found"...

It's likely because of the /translation-server prefix in URI being sent in every request to node.

Thanks very much for your help on this. Much appreciated; I've been completely in the dark aside from your guidance!

No problem.

That seems to have done it. It's now up and running. Amazing! Thanks again!