美文网首页
nodejs的process env config

nodejs的process env config

作者: 夏liao夏天 | 来源:发表于2018-10-06 10:55 被阅读0次

config的用途是为了运行和部署应用更加的方便。使用npm config package,可以自行定义默认值,并且可以扩展到不同的部署环境。

安装和default.json设置

$ npm install config
$ mkdir config
$ vi config/default.json
{
  "server": {
    "port": 4001
  }
}

production.json设置

$ vi config/production.json
{
  "server": {
    "port": 4002
  }
}

在nodejs中使用config

const app = require("./app");
const config = require("config");
let port = config.get("server.port");

app.listen(port, () => console.log(`listening on ${port}`));

启动nodejs的server

$ nodemon server

此时还未设置process.env.NODE_ENV, 因此用的是默认的设置。


image.png

设置NODE_ENV为production, 再次运行,可以看到port已经变成4002了。

$ export NODE_ENV=production
$ nodemon server
image.png

相关文章

网友评论

      本文标题:nodejs的process env config

      本文链接:https://www.haomeiwen.com/subject/zsncaftx.html