Score:0

NodeJs app working when started using npm start, but not with PM2

in flag

Very new to server stuff, sorry if this may be a noob question.

Problem

I am trying to run React and Node/express on a server. React does work and when I start my node app via npm start the server actually works too, meaning: Opening the website on IP w.x.y.z shows my website and using a form submit will reach the node backend. When I try to use PM2 to run my node app it does not work. PM2 will start my application but I can't reach it, neither through the website nor via curl localhost/api/....


Working example with npm start

Output of starting my node app with npm start:
Express server listening on port 3001
Then using curl I receive an expected message from the backend:
curl localhost/api/users -> {message: "blabla"}


Failing example with PM2 start and status

With npm start currently not running I start the application with the following command:
sudo pm2 start app.js -- start which then shows me it's online:

┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0  │ app                │ fork     │ 0    │ online    │ 0%       │ 10.5mb   │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘

But now using curl I receive a 503 server error:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>503 Service Unavailable</title>
</head><body>
<h1>Service Unavailable</h1>
<p>The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.</p>
<hr>
<address>Apache/2.4.48 (Ubuntu) Server at localhost Port 80</address>
</body></html>

Server at localhost Port 80 already made me wonder whether this indicates a problem but let's move on.


File structure

Static react files -> /var/www/html/client/build/
Node stuff -> /var/www/html/server/


Apache config

The config was changed to point to the build folder of the react app, also allowing the react router to work properly.

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/client/build/

        # following is used for React Router to work properly
        <Directory "/var/www/html/client/build/">
                RewriteEngine on
                RewriteCond %{REQUEST_FILENAME} -f [OR]
                RewriteCond %{REQUEST_FILENAME} -d
                RewriteRule ^ - [L]
                RewriteRule ^ index.html [L]
        </Directory>

        # following is what I thought is the right way to add the possibility to talk to the nodejs backend
        # ServerName localhost
        ProxyPreserveHost on

        ProxyPass /api http://localhost:3001/api
        ProxyPassReverse /api http://localhost:3001/api
...
</VirtualHost>

Netstat output

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 :::80                   :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN

UFW status output

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
80                         ALLOW       Anywhere
443                        ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
8080                       ALLOW       Anywhere
3001                       ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)
8080 (v6)                  ALLOW       Anywhere (v6)
3001 (v6)                  ALLOW       Anywhere (v6)

What have I tried so far

I looked at several setup examples for PM2 which are often just the basic starting of an app and don't go in too deep or don't have the combination with React.

I also tried to change the apache ProxyPass setting, adding the 3001 port to the allowed ports in UFW (does this make any sense when using the proxypass?).

Not sure whether there is a concept I haven't understood at this point or if I am still missing something. Or maybe just have a wrong configuration at this point.

Any help is greatly appreciated!

Old Geezer avatar
us flag
@Tommy What was the mistake?
Tommy avatar
in flag
@OldGeezer Sorry, that is too far in the past. I will see if I can check out the server setup later tonight and maybe can remember what I have changed.
Old Geezer avatar
us flag
My case was a mysterious pm2 daemon running under root. I tried many things and managed to kill it, but didn't know the exact reasons. This is my second server with the same problem.
jp flag
`netstat` shows that there are no processes listening on the port `3001`. Either your application isn't running or it is not listening on the port `3001`.
Tommy avatar
in flag
Ok, then this might be the problem. I already tried using the port flag like this `sudo pm2 start app.js -- start --port 3001` which does not change anything. Could this be a problem in my node app?
jp flag
You need to ensure that your application is 1) running 2) listening on port 3001. You can verify this with `netstat`.
Tommy avatar
in flag
Your comments led me to the (stupid) solution, it was a simple mistake launching the app with PM2. You may want to add it as an answer so I can accept it as the solution :), I will then comment the exact problem I found.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.