JavaScript structure node.js App with modules structure node in c,structure node in,node structure for

I want to structure my Node.js app with modules. For example I use bunyan as a logger.

I have a file main.js

//configs
var logfile = __dirname + '/log.txt';

//my own modules
var log = require("./modules/log"),
    init = require("./modules/init");

So I´m passing the log file + path to the log module.

My log.js

var bunyan    = require('bunyan');

function init(logfile){

var log = bunyan.createLogger({
  name: "myapp",
  streams: [{
    path: logfile
  }]
});
  return log;
};

module.exports.init = init;

And the init.js which at the moment only catches uncaught exceptions:

var logging = require('./log'),
    log = logging.init;

process.on('uncaughtException', function(err) {

  //print to console
  console.error((new Date).toUTCString() + ' uncaughtException:', err.message);
  console.error(err.stack);

  //print to bunyan logger
  log.error((new Date).toUTCString() + ' uncaughtException:', err.message);
  log.error(err.stack);

});

My main problem now is that I want to use the logger in every module. But when I do

var logging = require('./log'),
    log = logging.init;

in every file then it would create the logger every time. I just want to initialize it once and then use it in every module.

I thought about to remove the function for the logger:

log.js

var bunyan    = require('bunyan');
var log = bunyan.createLogger({
  name: "juntidos",
  streams: [{
    path: logfile
  }]
});

When I load the module it would be called only once. But how can I pass the filename parameter then?

Answer:1

I'm creating a CMS system using CKEditor and Dust javascript template, user will enter Dust template in ckeditor, and save to database, template will be something like {#News} <li><a herf=...

I'm creating a CMS system using CKEditor and Dust javascript template, user will enter Dust template in ckeditor, and save to database, template will be something like {#News} <li><a herf=...

i have a html file converted by swiffy that looks like this: <!doctype html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=...

i have a html file converted by swiffy that looks like this: <!doctype html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=...

I have built apache cordova using the npm functions and have opened it in Eclipse, I have edited it and so far the changes seem to be working okay. I want to now set up some bindings on the buttons I ...

I have built apache cordova using the npm functions and have opened it in Eclipse, I have edited it and so far the changes seem to be working okay. I want to now set up some bindings on the buttons I ...

  1. detect apache struts
  2. detect apache version
  3. detect apache version remotely
  4. detect apache version online
  5. detect apache struts version
  6. detect apache struts vulnerability
  7. detect apache tomcat version
  8. detect apache
  9. detect apache version nmap
  10. detect apache attack
  11. detect apache config
  12. detect apache webserver

Given access to valid audio files : <audio src="https://upload.wikimedia.org/wikipedia/commons/9/9b/Zh-Tianjin.ogg"></audio> <audio src="https://upload.wikimedia.org/wikipedia/commons/...

Given access to valid audio files : <audio src="https://upload.wikimedia.org/wikipedia/commons/9/9b/Zh-Tianjin.ogg"></audio> <audio src="https://upload.wikimedia.org/wikipedia/commons/...