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

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.

Related

How to write a simple Node.js App that taks dump output to browser?

I familiar to Node.js and just a little bit about web programming. One of my CLI Node app need to let user see the running program logs. I hope she can open a separated browser and point to something like http://localhost:12345, then she got live log keeping scrolling in the page without any human interaction.
Is that a simple method to do such kind of application? I know programming RESTful, not sure if it helps.
If I understood your question correctly, you are trying to show live server side logs to the user. For that you will have to tail the log file and pipe the output to response or pipe the stdout (if you're not writing logs to file) to the response using socket.io connection. socket.io is a way of providing live updates to the users without them sending https request every time. You can see example here .

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

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

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.

Can I log socket.io debug information to a file?

I'm running node.js with socket.io and I'd like to log the debug information (the information that shows in the node.js console when you run socket.io with default options) to a file, is there any way to do this?

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.

Resources