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

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

    create two files e.g app.js and tools.js

    app.js

    const tools= require("./tools.js")
    
    
    var x = tools.add(4,2) ;
    
    var y = tools.subtract(4,2);
    
    
    console.log(x);
    console.log(y);
    

    tools.js

     const add = function(x, y){
            return x+y;
        }
     const subtract = function(x, y){
                return x-y;
        }
    
        module.exports ={
            add,subtract
        }
    

    output

    6
    2
    

提交回复
热议问题