一、调用普通函数
声明函数:
function fun1(res) {
console.log("fun1");
res.write("I'm fun1");
}
在同一文件内调用:
fun1(response);
二、调用其它文件中的函数
声明函数并导出:
function fun2(res) {
console.log('我是fun2');
res.write('I'm fun2');
}
module.exports = fun2;
引入模块:
var otherfun = require('./models/otherfuns');
'./models/otherfuns.js' 表示fun2的所在文件路径
调用函数:
otherfun.fun2(response);
三、导出多个函数
module.exports = {
fun2:function(res){
console.log('我是fun2');
res.write('你好,我是fun2');
},
fun3:function(res){
console.log('我是fun3');
res.write('你好,我是fun3');
}
}
四、用字符串调用对应的函数
otherfun['fun2'](response);
otherfun['fun3'](response);