Define variables out of a file


I just discovered Sails.js, a MVC framework for Node.js. A controller is a file who export an object associating a method name to a function, il looks like this :

module.exports = {
   method : function(req, res) {
  /* ... */

However, in these functions, we can use models, defined in other files, used like this in the functions :

method: function(req, res) {
    Message.create(/* ... */);

The Message variable isn't defined in the file, no require() in the top of the file, so how it can be done ?

Problem courtesy of: palra


Sails.js has a lot of "automagic" going on under the covers. By default, sails will expose models, adapters and services to the global context of your controller module:

If you don't like this (and you shouldn't because we all know globals are evil), then you can turn it off through configuration, and manually require the models from the correct directory.

Solution courtesy of: srquinn


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.