nodejs中函数的调用实例详解
一、调用本射流研究…文件中的函数
var http=require(' http ');http.createServer(函数(请求,响应){ response.writeHead(200,{ ' Contet-Type ' : ' text/html;charset=utf-8 ' });if(request.url!=='/favicon.ico'){ funl(响应);回应。end(" ");} }).听(8000);console.log('运行于http://127.0.0.1:8000/'的服务器);funl(res)函数{控制台。日志(' fun 1 ');res.write('你好,我是乐趣1’;}运行结果:
二、调用外部的射流研究…文件
函数fun2(res){ console.log('我是,好玩2 ';res.write('你好我是乐趣2’;}//想把此射流研究…声明为一个函数,加下面代码,只适用于文件中只有一个函数module . exports=fun 2 var http=require(' http ');//ortherFun就代替了fun2var ortherFun=require(' ././其他js/out。js’);http.createServer(函数(请求,响应){ response.writeHead(200,{ ' Contet-Type ' : ' text/html;charset=utf-8 ' });if(request.url!=='/favicon.ico'){ //funl(响应);ortherFun(回应);回应。end(" ");} }).听(8000);console.log('运行于http://127.0.0.1:8000/'的服务器);funl(res)函数{控制台。日志(' fun 1 ');res.write('你好,我是乐趣1’;}
外部射流研究…文件内有多个函数
//支持多个函数模块。导出={ fun 2: function(RES){ console。日志('我是乐趣2’;res.write('你好,我是乐趣2’;},好玩的3:功能(RES){控制台。日志('我是乐趣3’;res.write('你好,我是乐趣3’;} } var http=require(' http ');var ortherFun=require(' ././其他js/out。js’);http.createServer(函数(请求,响应){ response.writeHead(200,{ ' Contet-Type ' : ' text/html;charset=utf-8 ' });if(request.url!=='/favicon.ico'){ //funl(响应);//ortherFun(响应);ortherFun.fun2(回应);ortherFun.fun3(回应);回应。end(" ");} }).听(8000);console.log('运行于http://127.0.0.1:8000/'的服务器);funl(res)函数{控制台。日志(' fun 1 ');res.write('你好,我是乐趣1’;}用字符串调用对应的函数
var http=require(' http ');var ortherFun=require(' ././其他js/out。js’);http.createServer(函数(请求,响应){ response.writeHead(200,{ ' Contet-Type ' : ' text/html;charset=utf-8 ' });if(request.url!=='/favicon.ico'){ //funl(响应);//ortherFun(响应);//ortherFun.fun2(响应);//ortherFun.fun3(响应);//用字符串调用对应的函数//ortherFun['fun2'](响应);//ortherFun['fun3'](响应);//还可以写成下面这样fun name=' fun 2 ortherfun[fun name](回应);回应。end(" ");} }).听(8000);console.log('运行于http://127.0.0.1:8000/'的服务器);funl(res)函数{控制台。日志(' fun 1 ');res.write('你好,我是乐趣1’;}
总结
以上所述是小编给大家介绍的开发中函数的调用实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
版权声明:nodejs中函数的调用实例详解是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。