nodejs中函数的调用实例详解


Posted in NodeJs onOctober 31, 2018

一、调用本js文件中的函数

var http = require('http');
http.createServer(function (request,response){
 response.writeHead(200, {'Contet-Type':'text/html;charset=utf-8'});
 
 if(request.url!=='/favicon.ico'){
 funl(response);
 response.end('');
 } 
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
function funl(res){
 console.log('fun1');
 res.write('hello ,我是fun1');
}

运行结果:

nodejs中函数的调用实例详解

nodejs中函数的调用实例详解

二、调用外部的js文件

nodejs中函数的调用实例详解nodejs中函数的调用实例详解

function fun2(res){
 console.log('我是,fun2');
 res.write('你好我是fun2');
}
// 想把此js声明为一个函数,加下面代码,只适用于文件中只有一个函数
module.exports = fun2;
var http = require('http');
// ortherFun 就代替了fun2
var ortherFun = require('./../otherjs/out.js');
http.createServer(function (request,response){
 response.writeHead(200, {'Contet-Type':'text/html;charset=utf-8'});
 
 if(request.url!=='/favicon.ico'){
 // funl(response);
 ortherFun(response);
 response.end('');
 } 
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
function funl(res){
 console.log('fun1');
 res.write('hello ,我是fun1');
}

nodejs中函数的调用实例详解

nodejs中函数的调用实例详解

外部js文件内有多个函数

// 支持多个函数
module.exports={
 fun2:function(res){
 console.log('我是fun2');
 res.write('你好,我是fun2');
 },
 fun3:function(res){
 console.log('我是fun3');
 res.write('你好,我是fun3');
 }
}
var http = require('http');
var ortherFun = require('./../otherjs/out.js');
http.createServer(function (request,response){
 response.writeHead(200, {'Contet-Type':'text/html;charset=utf-8'});
 
 if(request.url!=='/favicon.ico'){
 // funl(response);
 // ortherFun(response);
 ortherFun.fun2(response);
 ortherFun.fun3(response);
 response.end('');
 } 
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
function funl(res){
 console.log('fun1');
 res.write('hello ,我是fun1');
}

用字符串调用对应的函数

var http = require('http');
var ortherFun = require('./../otherjs/out.js');
http.createServer(function (request,response){
 response.writeHead(200, {'Contet-Type':'text/html;charset=utf-8'});
 
 if(request.url!=='/favicon.ico'){
 // funl(response);
 // ortherFun(response);
 //ortherFun.fun2(response);
 //ortherFun.fun3(response);
 
 // 用字符串调用对应的函数
 //ortherFun['fun2'](response);
 //ortherFun['fun3'](response);
    // 还可以写成下面这样
    funname = 'fun2';
    ortherFun[funname](response);
 response.end('');
 } 
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
function funl(res){
 console.log('fun1');
 res.write('hello ,我是fun1');
}

nodejs中函数的调用实例详解

nodejs中函数的调用实例详解

总结

以上所述是小编给大家介绍的nodejs中函数的调用实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

NodeJs 相关文章推荐
NodeJS的模块写法入门(实例代码)
Mar 07 NodeJs
利用NodeJS的子进程(child_process)调用系统命令的方法分享
Jun 05 NodeJs
跟我学Nodejs(三)--- Node.js模块
May 25 NodeJs
nodejs之请求路由概述
Jul 05 NodeJs
NodeJS学习笔记之MongoDB模块
Jan 13 NodeJs
Nodejs实战心得之eventproxy模块控制并发
Oct 27 NodeJs
详解Nodejs的timers模块
Dec 22 NodeJs
Highcharts+NodeJS搭建数据可视化平台示例
Jan 01 NodeJs
详解nodejs微信公众号开发——2.自动回复
Apr 10 NodeJs
nodejs个人博客开发第七步 后台登陆
Apr 12 NodeJs
详解Nodejs之静态资源处理
Jun 05 NodeJs
nodejs的路径问题的解决
Jun 30 NodeJs
NodeJS 将文件夹按照存放路径变成一个对应的JSON的方法
Oct 17 #NodeJs
Nodejs实现多文件夹文件同步
Oct 17 #NodeJs
深入理解NodeJS 多进程和集群
Oct 17 #NodeJs
CentOS7中源码编译安装NodeJS的完整步骤
Oct 13 #NodeJs
NodeJS加密解密及node-rsa加密解密用法详解
Oct 12 #NodeJs
NodeJS使用Range请求实现下载功能的方法示例
Oct 12 #NodeJs
nodejs实现范围请求的实现代码
Oct 12 #NodeJs
You might like
针对初学PHP者的疑难问答(2)
2006/10/09 PHP
Laravel框架自定义公共函数的引入操作示例
2019/04/16 PHP
javascript Firefox与IE 替换节点的方法
2010/02/24 Javascript
jquery学习笔记 用jquery实现无刷新登录
2011/08/08 Javascript
JavaScript eval() 函数介绍及应用示例
2014/07/29 Javascript
PHP守护进程实例
2015/03/06 Javascript
javascript中in运算符用法分析
2015/04/28 Javascript
JS插件overlib用法实例详解
2015/12/26 Javascript
Javascript的表单与验证-非空验证
2016/03/18 Javascript
JS Array创建及concat()split()slice()的使用方法
2016/06/03 Javascript
JS正则子匹配实例分析
2016/12/22 Javascript
angular+ionic 的app上拉加载更新数据实现方法
2017/01/16 Javascript
AngularJS中controller控制器继承的使用方法
2017/11/03 Javascript
Vue 组件传值几种常用方法【总结】
2018/05/28 Javascript
AngularJs的UI组件ui-Bootstrap之Tooltip和Popover
2018/07/13 Javascript
Vue.js中使用Vuex实现组件数据共享案例
2020/07/31 Javascript
详解微信小程序轨迹回放实现及遇到的坑
2021/02/02 Javascript
Python计算程序运行时间的方法
2014/12/13 Python
Python复制目录结构脚本代码分享
2015/03/06 Python
详解JavaScript编程中的window与window.screen对象
2015/10/26 Python
Python深入06——python的内存管理详解
2016/12/07 Python
浅谈django三种缓存模式的使用及注意点
2018/09/30 Python
python获取中文字符串长度的方法
2018/11/14 Python
python获取本机所有IP地址的方法
2018/12/26 Python
pyqt5 实现 下拉菜单 + 打开文件的示例代码
2019/06/20 Python
用openCV和Python 实现图片对比,并标识出不同点的方式
2019/12/19 Python
Python字符编码转码之GBK,UTF8互转
2020/02/09 Python
调整Jupyter notebook的启动目录操作
2020/04/10 Python
终于搞懂了Keras中multiloss的对应关系介绍
2020/06/22 Python
python向xls写入数据(包括合并,边框,对齐,列宽)
2021/02/02 Python
历史系毕业生自荐信
2013/10/28 职场文书
《美丽的田园》教学反思
2014/03/01 职场文书
优秀学生评语大全
2014/04/25 职场文书
思想作风建设心得体会
2014/10/22 职场文书
员工2014年度工作总结
2014/12/09 职场文书
个人工作违纪检讨书
2015/05/05 职场文书