How can I pass a variable while using `require` in node.js?

前端 未结 4 1471
生来不讨喜
生来不讨喜 2020-12-13 13:54

In my app.js I have below 3 lines.

var database = require(\'./database.js\');
var client = database.client
var user = require(\'./user.js\')         


        
4条回答
  •  自闭症患者
    2020-12-13 14:11

    You should just put the same code in user.js

    app.js

    var client = require('./database.js').client; // if you need client here at all
    var user = require('./user.js');
    

    user.js

    var client= require('./database.js').client;
    exports.find = function(id){
      //client.query.....
    }
    

    I don't see any drawbacks by doing it like this...

提交回复
热议问题