Warm tip: This article is reproduced from serverfault.com, please click

nodejs server only works when started from cli

发布于 2021-01-13 23:19:55

I'm trying to run a node app on my personal blog, which does display the static content, but cannot send POST calls back to the server when I start the server via systemctl. However when I run node index the server starts up and can send POST requests with JSON back to the server. How can I fix my systemd unit to properly start it up with equivalent ability to simply typing node index?

Systemctl service file:

[Unit]
Description=systemd node unit
Documentation=https://XXXX.com
After=network.target

[Service]
Environment=NODE_PORT=7070
Type=simple
User=ubuntu
ExecStart=/usr/bin/node /XXXX/XXXXXX/XXXXXXXX/XX/index.js
Restart=on-failure

[Install]
WantedBy=multi-user.target

Additional context:

-to route to my nodejs server, I use an nginx reverse proxy(:7070->example.com/something)

-my nodejs application functions perfectly fine on my local computer with the node index command

-node -v yields me:

v10.15.0
Questioner
Jake t
Viewed
22
Michael Hampton 2019-01-25 04:54:49

A lot of Node.js apps expect to have their working directory be the directory in which they are stored. But your systemd unit doesn't change the working directory.

You should set the working directory appropriately.

[Service]
WorkingDirectory=/wherever/you/installed/it