node - the development and production environments

What is the development and production environments

Environment, meaning the local operation of the project, when the project is in the development stage, the project is running on a computer developer, the environment in which the project is the development environment. When the project is developed, we want to project into the real web server running on the computer, the environment in which the project is the production environment.

Why distinguish between development and production environments

In different environments, the configuration item is not the same, is necessary to determine the environment in the project currently running program code configured according to different application environments project.

How to distinguish between development and production environments

The current development environment or a production environment is through the computer operating system environment variables in the system to distinguish.

Set the environment variable in the system:

NODE_ENV

  • development development environment

  • production production environment

Picture 2

morgan

node.js third-party modules, but also express middleware function, you can request information printed to the console for developers to view it

// 导入morgan这个第三方模块
const morgan = require('morgan');


// 获取系统环境变量 返回值是对象 
if (process.env.NODE_ENV == 'development') {
	// 当前是开发环境
	console.log('当前是开发环境')
	// 在开发环境中 将客户端发送到服务器端的请求信息打印到控制台中
	app.use(morgan('dev'))
} else {
	// 当前是生产环境
	console.log('当前是生产环境')
}

Third-party modules config

Action: allows developers to the application configuration information in different operating environments detached into separate files, the module automatically determines the internal operating environment of the current application, and read the corresponding configuration information, the application provides a great maintenance cost configuration information avoided when the switching times of the repeated operating environment, the project code manually to the modified configuration information

Steps for usage

  1. Use npm install config command module download

  2. New config folder in the root directory of the project

  3. In the config folder under the new default.json, development.json, production.json file

  4. , Introduced through the module to the project require methods

  5. The method of using the get inside the module configuration information

Sensitive configuration information stored in the environment variable

  1. Build custom-environment-variables.json config file in the folder

  2. Configuration property values ​​fill system environment variable name

  3. config module searches the system environment variables when the project runs and reads its value as the value belongs to the current configuration items

 { 
     "db": {
           "pwd": "APP_PWD"
     }
 }

Guess you like

Origin www.cnblogs.com/royal6/p/12584313.html