Home > Web Front-end > JS Tutorial > How to configure files using Node (detailed tutorial)

How to configure files using Node (detailed tutorial)

亚连
Release: 2018-06-14 11:34:23
Original
2457 people have browsed it

This article mainly introduces you to the relevant information about the use of configuration files in different environments of Node. The article introduces it in detail through sample codes. It has certain reference learning value for everyone's study or work. Friends who need it Let’s study together below.

Preface

When writing a complete project using Node.js, some configurable variables are often needed in the program, so that It allows the program to run in different environments. As we all know, in actual projects, there will be multiple different environments. In different environments, some configurations are different. How to call different configurations in different environments to improve development efficiency? Not much to say below, let’s take a look at the detailed introduction.

1. config-lite module

First introduce a configuration module config-lite and use the command npm i config-lite --save to install it .

Usually we will write the configuration into different configuration files for different environments. Create a new config directory under the Node project, and create new configuration files for different environments. Here I use "development" and "production" Two environments are used as examples to explain how to operate.

In the development environment, we create two new files in the config directory: test.js and default.js. You may ask why there is no production machine configuration file, because the production machine configuration must be in the production environment. Create it again.

Write the following code in the test.js configuration file (PS. Here is the configuration of mysql as an example):

// test.js
module.exports = {
 mysql : {
 host: "localhost",
 user: "lupeng",
 password: "080910",
 database: "b1imd"
 }
};
Copy after login

Write some default configuration files in default.js, such as session configuration, etc.

// default.js
module.exports = {
 mysql : {
 host: "10.20.141.220",
 user: "lupeng",
 password: "123456",
 database: "b1imd"
 },
 session: {
 secret: 'keyboard cat',
 resave: false,
 saveUninitialized: true,
 cookie: {
  maxAge: 1000*60*60
 }
 }
};
Copy after login

Okay, the configuration file is written, how to use it? Here we use the config-lite module. This module selects different configuration files based on environment variables, so we need to modify the startup command in package.json before using it:

"scripts": {
 "start": "NODE_ENV=production supervisor --harmony -i views/ ./bin/www",
 "test": "NODE_ENV=test supervisor --harmony -i views/ ./bin/www"
 },
Copy after login

As you can see, above There are two startup commands, one is for the production machine, set NODE_ENV=production, and the other is for the test machine, set NODE_ENV=test, when we use npm test to start the project At this time, config-lite will grab the test.js configuration and merge it with the configuration in default.js. If there are the same objects, the configuration in default.js will be overwritten. The above examples all have mysql objects, so the objects in test.js will prevail here.

Okay, let’s introduce how to use the config-lite module in the project. The code in app.js is as follows:

// 省略...
var config = require('config-lite')(__dirname);
// 省略...
app.use(session(config.session));
console.log("mysql服务器:" + config.mysql.host); // display mysql-config
// 省略...
Copy after login

After introduction, you can directly use the configuration object in the configuration file .

2. Production environment

So how to use it in the production environment? The basic principles and usage of config-lite have been introduced above. In the production environment, we only need to create a new production.js file in the config directory of the production machine environment, and then use the startup command npm start.

In order to avoid confusion between the test environment and the production environment configuration files, you can ignore the configuration files through the .gitignore file and add the following:

# config
config/*
!config/default.*
Copy after login

In this way, git will ignore configurations other than default.js File, in the local development environment, you can create multiple configuration files for testing and use, just set the corresponding environment variables. It should be noted that the environment variable name must be the same as the configuration file name.

3. windows environment

Maybe you are a multi-system environment developer and may develop in Linux and windows environments at the same time. Since windows The syntax for setting environment variables is different, so you can add two sentences to the package.json startup command, as follows:

"scripts": {
 "start": "NODE_ENV=production supervisor --harmony -i views/ ./bin/www",
 "test": "NODE_ENV=test supervisor --harmony -i views/ ./bin/www",
 "winStart": "SET NODE_ENV=production&&supervisor --harmony -i views/ ./bin/www",
 "winTest": "SET NODE_ENV=test&&supervisor --harmony -i views/ ./bin/www" 
 }
Copy after login

In this way, if deployed in a windows environment, create a new production in the config directory .js configuration file, start the command npm winStart; If deployed in a Linux or Unix-like environment, also create production.js, just start the command npm start.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed interpretation of React Native Flexbox layout

What are the methods for reference paths in vue single files?

Detailed introduction to the transformation of webpack.config.js in weex

The above is the detailed content of How to configure files using Node (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template