配置多種場景
我們的系統是要在不同的環境下運行的,不同的環境可能是不同的端口號,不同的數據庫地址,數據庫用戶和密碼,
並且可能有的需要自動重啟有的不需要自動重啟。
我們在有一個配置的時候是無法滿足這么多的場景的,所以我們要為不同的場景做額外配置
場景的種類
場景的種類一般就以下幾種
- 開發場景
- 真實場景
- 測試場景
-
debug場景
或者再有其他場景就做額外的添加,配制方法都是一樣的
開始配置
配置環境
我們可以通過對環境的NODE_ENV賦值來區分不同的環境
我們在通過命令行在運行程序的時候對其進行賦值,不同的場景賦值不一樣,在程序中讀取這個值來判斷是那種場景
首先我們修改package.json的script為:
1 "scripts": { 2 "start":"set NODE_ENV=production&&node bin/www", 3 "pm2": "pm2 start bin/www ", 4 "run":"set NODE_ENV=development&&nodemon bin/www", 5 "test": "set NODE_ENV=test&&echo \"Error: no test specified\" && exit 1" 6 },
(注意:我們將run 那段話的命令修改了)
這樣我們在通過不同的命令啟動程序的時候就NODE_ENV的值就是不同的我們就可以根據這個來設置不同的配置
配置不同的配置文件
新建文件夾config
新建三個文件
development.js
1 'use strict'; 2 /** 3 * 開發環境配置文件 4 */ 5 var config = { 6 env: 'development', //環境名稱 7 port: 3001, //服務端口號 8 mysql_config: { 9 //mysql數據庫配置 10 }, 11 mongodb_config: { 12 //mongodb數據庫配置 13 }, 14 redis_config: { 15 //redis數據庫配置 16 }, 17 }; 18 module.exports=config;
production.js
1 'use strict'; 2 /** 3 * 生產環境配置文件 4 */ 5 var config = { 6 env: 'production', //環境名稱 7 port: 3000, //服務端口號 8 mysql_config: { 9 //mysql數據庫配置 10 }, 11 mongodb_config: { 12 //mongodb數據庫配置 13 }, 14 redis_config: { 15 //redis數據庫配置 16 }, 17 18 }; 19 module.exports=config;
test.js
1 'use strict'; 2 /** 3 * 測試環境配置文件 4 */ 5 var config = { 6 env: 'test', //環境名稱 7 port: 3002, //服務端口號 8 mysql_config: { 9 //mysql數據庫配置 10 }, 11 mongodb_config: { 12 //mongodb數據庫配置 13 }, 14 redis_config: { 15 //redis緩存配置 16 }, 17 }; 18 module.exports=config;
當然了配置文件的內容可以按自己的喜好隨意設置。
統籌配置的文件config.js
我們需要單獨寫一個模塊來統籌這些配置並且判斷是哪種環境,並暴露對應配置。
這樣我們就在app_need文件夾內新建一個文件叫config.js
1 #!/usr/bin/env node 2 /** 3 * Module dependencies. 4 */ 5 var app = require('../app'); 6 var debug = require('debug')('demo:server'); 7 var http = require('http'); 8 /** 9 * Get port from environment and store in Express. 10 */ 11 var port = normalizePort(process.env.PORT || '3000'); 12 // app.set('port', port); 13 14 /** 15 * Create HTTP server. 16 */ 17 var server = http.createServer(app.callback()); 18 /** 19 * Listen on provided port, on all network interfaces. 20 */ 21 server.listen(port); 22 server.on('error', onError); 23 server.on('listening', onListening); 24 25 /** 26 * Normalize a port into a number, string, or false. 27 */ 28 function normalizePort(val) { 29 var port = parseInt(val, 10); 30 31 if (isNaN(port)) { 32 // named pipe 33 return val; 34 } 35 if (port >= 0) { 36 // port number 37 return port; 38 } 39 return false; 40 } 41 42 /** 43 * Event listener for HTTP server "error" event. 44 */ 45 function onError(error) { 46 if (error.syscall !== 'listen') { 47 throw error; 48 } 49 var bind = typeof port === 'string' 50 ? 'Pipe ' + port 51 : 'Port ' + port; 52 53 // handle specific listen errors with friendly messages 54 switch (error.code) { 55 case 'EACCES': 56 console.error(bind + ' requires elevated privileges'); 57 process.exit(1); 58 break; 59 case 'EADDRINUSE': 60 console.error(bind + ' is already in use'); 61 process.exit(1); 62 break; 63 default: 64 throw error; 65 } 66 } 67 /** 68 * Event listener for HTTP server "listening" event. 69 */ 70 71 function onListening() { 72 var addr = server.address(); 73 var bind = typeof addr === 'string' 74 ? 'pipe ' + addr 75 : 'port ' + addr.port; 76 debug('Listening on ' + bind); 77 }
我們找到
1 var port = normalizePort(process.env.PORT || '3000');
這句話,就是這句話聲明了端口號
我們就在這做文章
我們將其修改為:
1 //引入配置文件 2 var config = require('../app_need/config'); 3 console.log('當前運行環境為:'+config.env); 4 // 將端口號設置為配置文件的端口號,默認值為3000 5 var port = normalizePort(config.port || '3000'); 6 // 打印輸出端口號 7 console.log('當前監聽端口號為: ' + port);
這樣我們就通過配置和啟動命令的不同監聽不同的端口號,我們測試一下運行不同的命令看輸出
npm start
運行正常,輸出正常
npm run run
運行正常,輸出正常
npm test
運行正常,輸出正常(這里別忘了我們就是這么設置的
“test”: “set NODE_ENV=test&&echo \”Error: no test specified\” && exit 1”
所以輸出是正常的。這邊的test可以之后擴展。
配置文件的配置項目
一般情況把配置文件就當全局常量用就好了,因為我們除非手動改,所有的配置一般是不會變的,然后一般情況就是配置各種開關了,數據庫地址密碼啦,之類的。之后如果那個模塊需要用到配置了就可以調用config這個模塊讀取配置,通過不同的配置來做不同的事情, 覺得有用的大神關注相互關注下唄。