nodeJS调用函数

  

一、调用普通函数

声明函数:

function  fun1 (res), {   console.log才能(“fun1”);   res.write才能(“我'm  fun1”);   }

在同一文件内调用:

fun1(响应);


二、调用其它文件中的函数

声明函数并导出:

function  fun2 (res), {   console.log才能(“我是fun2”);   res.write才能(“我'm  fun2”);   }   module.exports =, fun2;

引入模块:

var  otherfun =,要求(“。/模型/otherfuns”);

’。/模型/otherfuns。js的表示fun2的所在文件路径

调用函数:

otherfun.fun2(响应);


三、导出多个函数

module.exports =, {   ,,,,,,,fun2:函数(res) {   console.log(“我是fun2 ');   res.write('你的好,我是fun2”);   },   fun3:函数(res) {   console.log(“我是fun3 ');   res.write('你的好,我是fun3”);   }   }


四,用字符串调用对应的函数

otherfun [' fun2 '](响应);   otherfun [' fun3 '](响应);



nodeJS调用函数