Controller File Updated on Nginx But Server still serves old content - node.js

I recently made some changes to one of my controllers on my nodejs application, the changes work fine locally, but when I deploy to the production server, though the changes reflect in the code on the server, it still serves the old version of the file. Am I missing out something?
I've tried restarting the server using
sudo systemctl restart nginx
Result stays the same.
Also tried redeploying, even tried to break the code and push it like that, yet all I get is the same old version. Any solution?
P.S I use PM2 in starting the app

I finally fixed it! Turns out I had to restart pm2 and not nginx. So I just ran
pm2 reload APP_NAME
And it updates
P.S You can know what APP_NAME is by running pm2 list

Related

How to stop and start node.js and update app.js?

I am trying to set up a node.js server. On my Mac, I can start and stop the app in terminal like:
start=
node app.js
stop=
CTR-c
I also have the same node setup on my linux server website.
I try to do the same start and stop in the terminal the same way on the linux server but I can't seem to stop it with CTR-c even though it looks like it did work.
The app keeps running.
My goal is to to be able to make edits / mods to the webserver node app. I think we have to stop and start to update the changes - but it does not seem to be working.
I know a bit about nodemon - but do not know if it should be used on live production servers?
Q: How do I make changes to the linux server app and get them to show up like they do in MacOS?

How to fix an issue that code changes are not reflecting in PM2?

In my PM2 instance running on Windows server having 4 cores CPU and that many clusters/instances, file changes are caught and servers get restarted but the changes do not get reflected. Instead it continues to show the old updates/features.
But when I tried doing the following it starts reflecting the new updates.
$ pm2 stop all
$ pm2 kill
$ pm2 start... (one by one)
This does not happen always but happens sometime only. How to fix this?
I've faced this problem in AWS deployment. Then I come up with a solution after long research and this worked for me as a charm.
After git pull just stop your server and then start it. After that
pm2 start index.js
pm2 reload index
Now your changes are reflected.

NodeJS changes not reflected on server

I'm running NodeJS application on CentOS with PM2. Everything works fine.
But I made a query changes in one of the controller file (add new column in SQL query). And then uploaded the project files to server. And then run PM2 restart [app-id].
It's strange that new column was not returned in the api response payload.
Is there anyway to clear cache of PM2? or What is the issue exactly?
Please advise!
For me worked sudo -u nodejs pm2 restart hello
more info:
https://marketplace.digitalocean.com/apps/nodejs#getting-started
Try running the following command.
pm2 stop app_name
pm2 restart app_name
pm2 reload app_name

ExpressJS Server Goes Offline Every Night - 502 Bad Gateway

I have a website with Nginx installed as a reserve proxy for an ExpressJS server (proxies to port 3001). This uses Node and ReactJS for my frontend application.
This is simply a testing website currently, and isn't known or used by any users. I have this installed on a Digital Ocean Droplet with Ubuntu.
Every morning when I wake up, I load my website and see 502 Bad Gateway. The problem is, I don't know how to find out how this happened. I have PM2 installed which should automatically restart my ExpressJS server but it hasn't done so, and when I run pm2 list, my application is still showing online:
When I run pm2 logs, I get the following error (I am running this as an Administrator):
So I'll run pm2 restart all to restart the app, but then I don't see any crash information. However on this occasion when taking this screenshot, there were a couple of unusual requests. /robots.txt, /sitemap.xml and /.well-known/security.txt, but nothing indicating a crash:
When I look at my Nginx error.log file, all I can see is the following:
There is, however, something obscure within my access.log ([09/Oct/2018:06:33:19 +0000]) but I have no idea what this means:
If I run curl localhost:3001 whilst the server is offline, I will receive a connection error message. This works fine after I run pm2 restart all.
I'm completely stuck with this and even the smallest bit of help would be appreciated greatly, even if it's just to tell me I'm barking up the wrong tree completely and need to look elsewhere - thank you.
I think you should check this github thread, it seems like it could help you.
Basically, after few hours, a Nodejs server stop functioning, and the poor nginx can not forward its requests, as the service listening to the forward port is dead. So it triggers a 502 error.
It was all due to a memory leak, that leads to a massive garbage collection, then to the server to crash. Check your memory consumption, you could have some surprises. And try to debug your app code, a piece (dependency) at the time.
Updated answer:
So, i will add another branch to my question as it seems it has not helped you so far.
You could try to get rid of pm2, and use systemd to manage your app life cycle.
Create a service file
sudo vim /lib/systemd/system/appname.service
this is a simple file i used myself for a random ExpressJS app:
[Unit]
Description=YourApp Site Server
[Service]
ExecStart=/home/user/appname/index.js
Restart=always
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/home/user/appname
[Install]
WantedBy=multi-user.target
Note that it will try to restart if it fails somehow Restart=always
Manage it with systemd
Register the new service with:
sudo systemctl daemon-reload
Now start your app from systemd with:
sudo systemctl start appname
from now on you should be able to manage your app life cycle with the usual systemd commands.
You could add stdout and stderr to syslog to understand what you app is doing
StandardOutput=syslog
StandardError=syslog
Hope it helps more
You cannot say when exactly NodeJS will crash, or will do big GC, or will stun for other reason.
Easiest way to cover such issues is to do health check and restart an app. This should not be an issue when working with cluster.
Please look at health check module implementation, you may try to use it, or write some simple shell script to do the check

Getting 502 error in browser for a NodeJS app run with Forever

I have a SailsJS app set up on a Webfaction server. Everything works nicely (site can be accessed through browser, console works) when I run the app via any of the following commands, with and without the --prod param:
sails console,
sails lift,
node app.js
However, when I try to run the app with forever using forever app.js I get a 502 error, as if nodejs server isn't even running. When I run forever list I can see app.js listed among running processes.
How can I have my app run with forever?
Forever is considered outdated by many in the Node community, and thankfully, has been replaced by several other fantastic (dare I say, better) tools.
If you're running a newer flavor of Ubuntu, you can always install systemd and kick off the application that way. If you're seeking something more streamlined, Phusion Passenger might be your ticket. It has a long track record of successes, and I wouldn't hesitate to toss it into production.
I managed to solve this issue; the problem occurred due to SailsJS migration prompt which shows up when you start the server. Running app.js with forever worked, but the server didn't start because the script hanged waiting for a prompt reply. If you encounter this issue just make sure you have your migrate option set in model config to avoid running into migration prompt.

Resources