torbenrudgaard torbenrudgaard - 1 year ago 125
Node.js Question

express logger, morgan logger etc. why do I need this in my server app?

I got a nodejs server, lots of routes, models and controllers but no frontend at all. The server only serves json results back to the calling angular pages.

Then I stumbled across this:


And this

var morgan = require('morgan')

I tried to read about them but all I can see is that they log all HTTP requests and responses, is that correct?

Do I really need them to run the server, or is this just for making very big log files of all traffic?

Also, are there any loggers that only log important errors. Like when an error is thrown. Would it be possible to have some kind of logger that catches everything that comes down here:

catch(err) {
console.log(" newBooking: " + err);
callback( { error:true, err } );

I have no experience with logging stuff on a nodejs server, so any help would be appreciated.

Answer Source

You don't need a (request) logger if you don't want to (although it's sometimes nice to have in case something goes wrong, or your server is getting a lot of requests from a particular IP-address).

Since you're using Express by the looks of it, you can add a custom error handler (documented here) that will get called whenever an error happens:

app.use((err, req, res, next) => {
  console.log(" newBooking: " + err);
  res.json({ error:true, err : err.message });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download