在中间件中使用sails.config

时间:2016-05-26 07:47:39

标签: javascript node.js sails.js

我在app / config / http.js中使用带有sails.js的头盔模块进行内容安全策略

// app/config/http.js
module.exports.http = {
    order: [
      ...
      'csp',
      'router',
      'www',
      'favicon',
      '404',
      '500'
    ],
    ...
    csp: require('helmet').csp({directives:{...}}),
    ...
}

我希望csp()中的对象位于我的config / local.js

// config/local.js
module.exports = {
  ...
  csp: {directives:{...}},
  ...
}

但如果我在app / config / http.js中require('helmet').csp(sails.config.csp) 我收到了一个错误:

  

ReferenceError:未定义风帆

我理解为什么没有定义,但我不知道如何根据环境更改sails.config.http

1 个答案:

答案 0 :(得分:1)

您不需要引用sails.config。你的local.js应该覆盖你在sails.http中设置的任何“默认值”。

例如:

// app/config/http.js
module.exports.http = {
    ...
    csp: require('helmet').csp({directives:{...}}), // default settings
    ...
}


// app/config/local.js
module.exports = {
    ...
    // custom http settings for this environment
    http: {
       csp: require('helmet').csp({directives:{...}}) 
   }
    ...
}