在sails.js中创建配置变量?

在sails.js中创建配置变量?,第1张

在sails.js中创建配置变量

您可以在

config/
文件夹中创建自己的配置文件。例如,
config/myconf.js
使用您的配置变量:

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

然后通过全局

sails
变量从任何视图访问这些变量。

在视图中:
<!-- views/foo/bar.ejs --><%= sails.config.myconf.name %><%= sails.config.myconf.author %>
在服务
// api/services/FooService.jsmodule.exports = {    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
在模型中:
// api/models/Foo.jsmodule.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)
在控制器中:

注意:这在策略中的工作方式相同。

// api/controllers/FooController.jsmodule.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)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5620162.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存