Martial
Martial

Reputation: 1562

using sails.config in middleware

I use helmet module with sails.js for Content Security Policy in app/config/http.js

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

I want the object in csp() to be in my config/local.js

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

But if I do require('helmet').csp(sails.config.csp) in app/config/http.js I got an error :

ReferenceError: sails is not defined

I understand why it is not defined, but I don't know how to change sails.config.http depending of the environment

Upvotes: 0

Views: 337

Answers (1)

Matt Wielbut
Matt Wielbut

Reputation: 2692

You shouldn't need to reference sails.config. Your local.js should just override any "defaults" you set in sails.http.

For example:

// 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:{...}}) 
   }
    ...
}

Upvotes: 1

Related Questions