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

How to configure files using Node (detailed tutorial)

Jun 14, 2018 am 11:34 AM
node nodejs Configuration file

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is nodejs a backend framework? Is nodejs a backend framework? Apr 21, 2024 am 05:09 AM

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

How to connect nodejs to mysql database How to connect nodejs to mysql database Apr 21, 2024 am 06:13 AM

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

What is the difference between npm and npm.cmd files in the nodejs installation directory? What is the difference between npm and npm.cmd files in the nodejs installation directory? Apr 21, 2024 am 05:18 AM

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

What are the global variables in nodejs What are the global variables in nodejs Apr 21, 2024 am 04:54 AM

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

Is there a big difference between nodejs and java? Is there a big difference between nodejs and java? Apr 21, 2024 am 06:12 AM

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

Where is the win10 user profile? How to set the user profile in Win10 Where is the win10 user profile? How to set the user profile in Win10 Jun 25, 2024 pm 05:55 PM

Recently, many Win10 system users want to change the user profile, but they don’t know how to do it. This article will show you how to set the user profile in Win10 system! How to set up user profile in Win10 1. First, press the "Win+I" keys to open the settings interface, and click to enter the "System" settings. 2. Then, in the opened interface, click "About" on the left, then find and click "Advanced System Settings". 3. Then, in the pop-up window, switch to the "" option bar and click "User Configuration" below.

Is nodejs a back-end development language? Is nodejs a back-end development language? Apr 21, 2024 am 05:09 AM

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

See all articles