Reading a .config file

匿名 (未验证) 提交于 2019-12-03 08:41:19

问题:

Currently I have a file called router.js set up as follows:

var Server = require('mongodb').Server; var MongoDB = require('mongodb').Db; var dbPort = 31979; var dbHost = '40.117.155.19'; var dbName = 'node-login';

I would like to to be set up like this:

var Server = require('mongodb').Server; var MongoDB = require('mongodb').Db; var dbPort      = readConfig(dbPort); var dbHost      = readConfig(dbHost); var dbName      = readConfig(dbName);

How would I go about accomplishing this. I would like to have a file such as test.config, and be able to read dbPort, dbHost, and dbName from that .config file in router.js.

回答1:

You could store your config as a JSON file and read it directly:

config.json

{     "dbPort": 31979,     "dbHost": "40.117.155.19",     "dbName": "node-login" }

router.js

var Server = require('mongodb').Server; var MongoDB = require('mongodb').Db; var CONFIG = require('./config.json');  var dbPort = CONFIG.dbPort; var dbHost = CONFIG.dbHost; var dbName = CONFIG.dbName;


回答2:

Here's one way to do it

//File config.js module.exports = {   dbPort : 8080,   dbHost : etc,   dbName : nom, }  //File server.js var Server  = require('mongodb').Server; var MongoDB = require('mongodb').Db; var config  = require('configFile');  var dbPort      = config.dbPort; var dbHost      = config.dbHost; var dbName      = config.dbName;


转载请标明出处:Reading a .config file
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!