When I connect to my Express 4 node.js websocket server from my client and try to log req, the whole program just gets stuck. It still accepts new connections and executes it to the same console.log, but then gets stuck. I am trying to figure out what req contains, but this way of figuring it out doesn't seem to work.
app.use(function (req, res, next) {
console.log("middleware");
var session = req.session;
console.log("session: " + JSON.stringify(session));
console.log("req non json: " + req);
console.log("req: " + JSON.stringify(req)); //stuck
return next();
});
What's happening here is that you're trying to stringify req, an object not stringifiable.
console.log()
accepts many arguments in the constructor, if you supply one argument and use +
what's happening is that you're trying to pass two strings into console.log()
stringifying or .toString()
the object instead of printing the object's content.
I'm gonna try and explain what you should do and why:
console.log("string as argument one", [object])
This is the desired effect you want, internally console.log()
will understand that you want to print the string and the object's content and not the .toString()
which most often is [Object object] or doesn't exist (not all objects have a .toString()
since they can be created out of different prototypes
).