1

Given the following situation: A Node.js express app with some routes and modules

app.js

/routes
|-user.js
|-login.js
|-e.t.c.

/modules
|-logger.js

Let's say that all the routes need the logger to do proper error handling and logging. Would it be better to include the logger in all the routes or only in app.js and then inject the logger object into the routes?

The latter would seem to be less resource heavy because the object only gets created once, but is this the case?

NG.
  • 459
  • 1
  • 6
  • 20
  • If anyone has a better title you are welcome to change it. I had a bit of trouble finding a good one. – NG. Oct 30 '17 at 11:38
  • If your logger is just `var logger = require('logger');` it won't make a performance difference as node will return the same instance of your module. – Roland Starke Oct 30 '17 at 11:43
  • so if the same file gets referenced multiple times in the code, it will only run it the first time and then just reference the object that was created from then on? – NG. Oct 30 '17 at 11:46
  • Yes. https://nodejs.org/docs/latest/api/modules.html#modules_caching – Roland Starke Oct 30 '17 at 11:51
  • I'm not sure what to do with this question now, should I close it or leave it open to other people? Since this is info that can be found in the docs.. – NG. Oct 30 '17 at 11:54
  • idk. I would still like to know how others handle this. like what if your logger needs a configuration like: `var logger = new require('logger')({path: 'error.log'});` – Roland Starke Oct 30 '17 at 11:56

0 Answers0