Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm config使用 #20

Open
koala-coding opened this issue Jul 14, 2019 · 0 comments
Open

npm config使用 #20

koala-coding opened this issue Jul 14, 2019 · 0 comments

Comments

@koala-coding
Copy link
Owner

环境变量

讲config之前,先说一下环境变量这个事。

Linux系统中查看所有环境变量

env

node中常用到的环境变量是NODE_ENV,查看是否存在

echo $NODE_ENV

Linux系统中添加环境变量

env

某些时候需要删除环境变量

unset NODE_ENV

环境变量追加值

export PATH=$PATH:/home/download:/usr/local/

config模块包使用

安装

npm install config

代码中使用

config安装成功后,可以在项目中创建几种环境的配置文件。

  1. test 测试环境
  2. product 生产环境
  3. development 开发环境
    以development环境举例:
{
  "port": 9001,
  "host": "http://localhost:9001",
  "redis":{
    "host": "localhost",
    "port": 6379,
    "password":"root",
    "db": 2,
    "connectTimeout": 3000,
    "prefix": "koala:"
  }
 }

代码中使用配置文件

const config=require('config');

app.use(require('./middleware/redis')(config.get('redis')));

开启app服务

  • 常规启动
export NODE_ENV=development
node app.js
  • 一条命令启动 app服务器
export NODE_ENV='development' && node app.js
```linux
- pm2指定名称与环境启动app服务

```linux
pm2 start app.js --env development --name koala

--name name指的是pm2指定的进程名称。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant