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

后端 未结 25 2535
猫巷女王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:08

    Create two js files

    // File cal.js
    module.exports = {
        sum: function(a,b) {
            return a+b
        },
        multiply: function(a,b) {
            return a*b
        }
    };
    

    Main js file

    // File app.js
    var tools = require("./cal.js");
    var value = tools.sum(10,20);
    console.log("Value: "+value);
    

    Console Output

    Value: 30
    

提交回复
热议问题