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

后端 未结 25 2896
猫巷女王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 04:53

    Another method when using node.js and express.js framework

    var f1 = function(){
       console.log("f1");
    }
    var f2 = function(){
       console.log("f2");
    }
    
    module.exports = {
       f1 : f1,
       f2 : f2
    }
    

    store this in a js file named s and in the folder statics

    Now to use the function

    var s = require('../statics/s');
    s.f1();
    s.f2();
    

提交回复
热议问题