In Node.js, how do I “include” functions from my other files?

后端 未结 25 2558
猫巷女王i
猫巷女王i 2020-11-22 04:22

Let\'s say I have a file called app.js. Pretty simple:

var express = require(\'express\');
var app = express.createServer();
app.set(\'views\', __dirname + \         


        
25条回答
  •  情话喂你
    2020-11-22 05:05

    It worked with me like the following....

    Lib1.js

    //Any other private code here 
    
    // Code you want to export
    exports.function1 = function(params) {.......};
    exports.function2 = function(params) {.......};
    
    // Again any private code
    

    now in the Main.js file you need to include Lib1.js

    var mylib = requires('lib1.js');
    mylib.function1(params);
    mylib.function2(params);
    

    Please remember to put the Lib1.js in node_modules folder.

提交回复
热议问题