How to access a Node.js application log lauched through pm2 - node.js

I've been developing a Node.js application using Socket.IO, Express, MySql and https and everything worked fine until I "deamonized" it with pm2. Now, my socket seems somehow unresponsive and I'd like to debug it. The problem is that I can't seem to find where the console.log() function from this code outputs its text anymore.
I case you'd like to know, all my pm2 processes are online and I can refresh my pages from the client side. But there should be a fonction on the server that triggers an event on the client side when something happens in the database and it does not.
Could tell me where the output from consone.log() is goes?

You can access the logs with the pm2 logs command
http://pm2.keymetrics.io/docs/usage/log-management/

the problem is that while your server is running and you use
pm2 log
all logs will be displayed except
console.log()
all you have to do is instead of pm2 log, run
pm2 logs
note the "s" at the logs. hope this helps

From PM2 docs:
PM2 allows you to easily manage your application’s logs. You can
display the logs coming from all your applications in real-time, flush
them, and reload them. There are also different ways to configure how
PM2 will handle your logs (separated in different files, merged, with
timestamp…) without modifying anything in your code.
http://pm2.keymetrics.io/docs/usage/log-management/
And other SO question:
Make pm2 log to console

Related

Express.js app stops without PM2 restarting it

I have an Express JS app that is run using PM2 behind a NGINX proxy. It works fine, except for when we send a push notification to all users and the server probably gets too much load. The problem though is that the app just hangs when this happens. Nginx logs reports Connection Refused, Timeouts. My express app doesn't report any errors when this happens so I can't really find what is happening when it hangs. PM2 doesn't restart the app automatically.
It kind of feels like there is a promise that's not getting resolved or rejected, but I have no proof of this. It's just a feeling. Also, should this be logged somewhere? Node usually warns that this will be different in the future.
I know my description is generic, but does anyone have any experience in similar problems? Or maybe can guide me how to find out more why it breaks?
Node 12,
Express 4,
Postgres 9,
pg-promise,
Nginx,
Ubuntu Linux

node-watch does not watch directory when the app runs via forever.js

I have an app that monitors a directory for file changes and the send a notification via socket.io to the user. and this works great when i run node app.js but when i start it with forever start /dir/to/app.js the application runs and you can connect to it from the browser but no notification from node-watch are received. No errors that im aware of show up.
You probably have "watch": true in your forever config file.
That will cause your whole app to restart whenever there is a file change, which will cause node-watch to be restarted with the current state of the filesystem, so node-watch will never be able to detect a change.
If your socket pool is stored outside the node process' memory (EG. using the socket.io-redis adapater), your browser client will automatically reconnect after the app restarts without any issues.

How do I find out where my Express app the response is being sent/finished?

I'm getting Error: Can't set headers after they are sent in my app. I know a response is being sent before the response I intend to deliver, but I'm not sure where that's coming from. Can I log to the console when a respnose has been sent? I'm currently using morgan for logging, but it only seems to log requests. I can't find a way to log every response as well.
If not, what is another effective way to hunt this problem down?
Update: This app runs on Azure and uses the Azure SDK for node or else I would try running it through the node debugger or node-inspector. I don't think there's a way to do this while also running the app in the emulator. I could be wrong about that.
Your error says that there was a response send already, check your code with the node-inspector or any other debugging tool and search for the spot.
Here the node-inspector.
And since you work with Azure this should help you getting started with the node-inspector.

restart nodejs server programmatically

User case:
My nodejs server start with a configuration wizard that allow user to change the port and scheme. Even more, update the express routes
Question:
Is it possible to apply the such kind of configuration changes on the fly? restart the server can definitely bring all the changes online but i'm not sure how to trigger it from code.
Changing core configuration on the fly is rarely practiced. Node.js and most http frameworks do not support it neither at this point.
Modifying configuration and then restarting the server is completley valid solution and I suggest you to use it.
To restart server programatically you have to execute logics outside of the node.js, so that this process can continue once node.js process is killed. Granted you are running node.js server on Linux, the Bash script sounds like the best tool available for you.
Implementation will look something like this:
Client presses a switch somewhere on your site powered by node.js
Node.js then executes some JavaScript code which instructs your OS to execute some bash script, lets say it is script.sh
script.sh restarts node.js
Done
If any of the steps is difficult, ask about it. Though step 1 is something you are likely handling yourself already.
I know this question was asked a long time ago but since I ran into this problem I will share what I ended up doing.
For my problem I needed to restart the server since the user is allowed to change the port on their website. What I ended up doing is wrapping the whole server creation (https.createServer/server.listen) into a function called startServer(port). I would call this function at the end of the file with a default port. The user would change port by accessing endpoint /changePort?port=3000. That endpoint would call another function called restartServer(server,res,port) which would then call the startServer(port) with the new port then redirect user to that new site with the new port.
Much better than restarting the whole nodejs process.

Socket.io - Easiest way to log values from the server

When you are testing your javascript on the server (server.js) file using socket.io, you can't use console.log(), or alert().
What is the easiest way to log or alert a value for testing purposes?
Of course you can use console.log
It just gets logged to the terminal.
Node Docs on console
I log to syslog via the node-syslog package that way I can still see the logs even when the server is running in the background.

Resources