简体   繁体   中英

How to define jade as a global variable in node.js Express?

Right now the following routing function works:

exports.summary = function(req, res, next) { 
  var jade = require('jade');
  res.render('myView', {
     main: jade.renderFile('./views/summary.jade')
  });
};

As you can see, the variable 'jade' is defined locally everytime the routing function is triggered. This can be a concern to performance. Whenever I tried to define jade globally, I received an error message saying jade is not defined.

Is there a way for me to define 'jade' globally?

s global.jade = require('jade');

Read more about global here .

But for your case it's better just to define jade in the module level:

var jade = require('jade');
exports.summary = function(req, res, next) { 
  res.render('myView', {
     main: jade.renderFile('./views/summary.jade')
  });
};

This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM