Reading a .config file

后端 未结 2 1991
天命终不由人
天命终不由人 2021-02-04 04:16

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;
         


        
2条回答
  •  一个人的身影
    2021-02-04 04:38

    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;
    

提交回复
热议问题