Does console.log not work within socket.io event? - node.js

Is there something that prevents a console.log() from firing within a socket.io event? I am having a frustrating time with them.
socket.on('messageReceived', function(data) {
console.log('Server ' + data.msg);
var clientData = {'msg', 'Hello server'};
socket.emit('clientResponse', clientData);
});
in that code my node server receives the client response, but my browser console does not output the console.log. I don't get why... is this normal?

You can try console.log('a') in console of browser.
If you don't see a, I think you changed level log of console (verbore, info, warn, error) or used filter (you can change it in under of tab console).
I don't know what happen if console priint a.

I figured out the problem. I was using socket.broadcast.emit from the server instead of broadcast.emit.
I noticed when another browser I had open outputted the console.log.
Thanks everyone who read and put thought into this.

I also had console.log not working and I have been spending the weekend debugging it. Nothing worked. (But my server was still running fine) Finally, I became fed up with it and I commented out my entire io.on('connection') code and it still ran.
Then, I went to my client and I saw that I still had my client sending and retrieving to and from my Heroku build that I set when I published the website.
So if you're having issues and nothing that you do is working, check the socket server that you're connecting to with your client.

Related

Why might a nodeJS API work locally but fail in production?

An API method I've been using without problems on localhost:3000/userinfo suddenly gives an "Empty Reply From Server" when I upload that same code to my web server and send a POST request to myserver.com:3000/userinfo.
Even more strange is that if the JSON returned by user info does not contain an array of objects, there is no error locally or remotely.
I don't know how much detail will be useful, and how much will just clutter this question. This one is especially strange so I'd like to approach it very generally. In other words, I don't think I'm missing a semi colon this time because the API function works perfectly when run on a local server.
So without a whole lot of detail, the only thing I can suggest here is make sure your ports are correct. You said 4200 locally but its 3000 on your server? The error you're getting also suggests ( I'm assuming cURL? ) that it couldn't connect at all to your server, which furthers my hypothesis.
Alternatively, there could be an error in your API and its "crashing" without logging anything, which would also cause an empty reply error.
Be sure to capture unhandled exceptions using process events to log out errors!
https://nodejs.org/dist/latest-v10.x/docs/api/process.html#process_process_events
process.on('uncaughtException', function (error) {
console.error("GOT UNCAUGHT EXECPTION!", error)
process.exit(1);
})

Socket.io .on(message) seems be delayed

I have server code like (simplified)
setInterval(longFunction,30);
setInterval(longFunction,30);
function longFunction(){
console.log("longFunction");
doSomethingLong();
}
elsewhere in server code...
socket.on("message",function(){
console.log("message");
})
Problem is that when I send message from client, I can see in server console many many new logs from longFunction and only then I can see log from message.
I want to process message event as soon as possible but it looks that server prefer processing of intervals even multiple times first.

ExpressJS-Localhost not showing anything

This is my first time I am running ExpressJS. But the Localhost is not responding any data.
Below is the Code I have written
const express=require('express');
const app=express();
app.get('/',(req,res)=>
{
});
app.listen(3000,()=>console.log("running at 3000 port"));
I have tried to change the localhost to 4000,5000.But same error.
Additional Information:
Running on Ubuntu 16.10
Any clue of whats happening over here?
You're not giving the browser anything to render. Try following the Hello World example in the docs and you might be pleasantly surprised:
https://expressjs.com/en/starter/hello-world.html
I dont know if you still need an answer but i think i might have it.
I had the same issue with the 'app.use(express.json)'
when i deleted my server started working, it was because we were writing it work it actually is
'app.use(express.json())'
PD: that command works for telling EXPRESS that it has to read a json file or request...
i hope this can help you out
Just to add to imjared's answer.. Always, response has to be sent back to the client(Browser) for any new page you create because that's what will be displayed on the browser. Adding res.send("some data") in the function will resolve the issue.

Viewing node console log remotely

I have been building my first node app. In testing on my mac, I was able to view the console log output using terminal.
I'm now moving the app to a server but I still want to get a live dump of the console log. Yes, I can get this by SSH'ing into the server - start the app then watch the output. But, say my SSH connection to the server gets disconnected. After re-connecting to the server, how do I go about viewing the terminal output of that process?
One solution I came across was http://console.re - this looks ideal, however it comes with warnings not to use in a production environment. Coupled with the fact that it's public, I'm hesitant to use it.
Does anyone know of an alternative solution similar to console.re?
Thanks
You could try using a custom function that writes the output to a log file, as well as printing it on screen.
Something like this: (note that this won't accept multiple arguments)
var fs = require('fs');
module.exports = function(text) {
fs.writeFile('console.log', text, {
flag: 'a' // append
}, function(){}); // ignore the response
console.log(text);
};
Perhaps screen, tmux, or similar software might work for you.

How to stop the preview server

This code is based on wintersmith static site generator
In my code I'm starting the preview server like this:
env.preview(function(error, server) {
if (error) throw error;
console.log('Server running!');
setTimeout(function(){
console.log("going to shut it down")
server.stop();
}, 3000)
});
This is taken from the example. I would expect the preview server to stop after 3 seconds, but instead I get an error, that server is undefined.
I looked a bit closer at it and indeed: server is undefined. Anything I'm doing wrong? How am I able to stop the preview server?
This looks like a bug, create an issue on github and i'll have a look at it when i get some time.

Resources