Access to NODE_ENV at runtime in server.js Sails.js configuration

I cannot access the NODE_ENV variable when I try to make configuration changes to the config/server.jsSails.js file . I want to set the serverOptionsproperty flag expressand bind ssl certificates - but only for production.

Assuming I started the server with node app.js --prod

This does not work:

module.exports = { ... };
console.log(process.env.NODE_ENV); // undefined

But it works:

module.exports = { ... };
setTimeout(function () {
  console.log(process.env.NODE_ENV); // production
}, 1000);

Basically all I want to do is:

module.exports = (function () {
  var env = process.env.NODE_ENV,
    ret = { express: {} };

  if (env == 'production') {
    ret.express.serverOptions = { key: ..., cert: ... };
  }

  return ret;
}());

I have seen similar answers, such as https://stackoverflow.com/a/3129381/2129 , but I can’t figure out how they should work when mine doesn’t.

+4
source share
1 answer

run your application as follows:

NODE_ENV=production node app.js

or

sails lift --prod

. , , javascript .

+7

Source: https://habr.com/ru/post/1523936/


All Articles