Create config variables in sails.js?

后端 未结 2 1002
傲寒
傲寒 2020-12-12 19:10

I\'m converting an app of mine from Express to sails.js - is there a way I can do something like this in Sails?

From my app.js file in Express:

相关标签:
2条回答
  • 2020-12-12 19:44

    You can create your own config file in config/ folder. For example config/myconf.js with your config variables:

    module.exports.myconf = {
        name: 'projectName',
        author: 'authorName',
    
        anyobject: {
          bar: "foo"
        }
    };
    

    and then access these variables from any view via global sails variable.

    In a view:

    <!-- views/foo/bar.ejs -->
    <%= sails.config.myconf.name %>
    <%= sails.config.myconf.author %>
    

    In a service

    // api/services/FooService.js
    module.exports = {
    
      /**
       * Some function that does stuff.
       *
       * @param  {[type]}   options [description]
       * @param  {Function} cb      [description]
       */
      lookupDumbledore: function(options, cb) {
    
        // `sails` object is available here:
        var conf = sails.config;
        cb(null, conf.whatever);
      }
    };
    
    // `sails` is not available out here
    // (it doesn't exist yet)
    console.log(sails);  // ==> undefined
    

    In a model:

    // api/models/Foo.js
    module.exports = {
      attributes: {
        // ...
      },
    
      someModelMethod: function (options, cb) {
    
        // `sails` object is available here:
        var conf = sails.config;
        cb(null, conf.whatever);
      }
    };
    
    // `sails is not available out here
    // (doesn't exist yet)
    

    In a controller:

    Note: This works the same way in policies.

    // api/controllers/FooController.js
    module.exports = {
      index: function (req, res) {
    
        // `sails` is available in here
    
        return res.json({
          name: sails.config.myconf.name
        });
      }
    };
    
    // `sails is not available out here
    // (doesn't exist yet)
    
    0 讨论(0)
  • 2020-12-12 20:08

    I just made a service which delivers a value:

    maxLimbs: function(){
            var maxLimbs = 15;
            return maxLimbs;
        }
    
    0 讨论(0)
提交回复
热议问题