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与Mysql的交互示例代码
Aug 18 NodeJs
轻松创建nodejs服务器(5):事件处理程序
Dec 18 NodeJs
nodejs批量修改文件编码格式
Jan 22 NodeJs
nodejs爬虫抓取数据乱码问题总结
Jul 03 NodeJs
详解nodejs 文本操作模块-fs模块(三)
Dec 22 NodeJs
nodejs 实现钉钉ISV接入的加密解密方法
Jan 16 NodeJs
配置nodejs环境的方法
May 13 NodeJs
Nodejs中使用captchapng模块生成图片验证码
May 18 NodeJs
nodejs密码加密中生成随机数的实例代码
Jul 17 NodeJs
NodeJS使用七牛云存储上传文件的方法
Jul 24 NodeJs
修改Nodejs内置的npm默认配置路径方法
May 13 NodeJs
NodeJS模块Buffer原理及使用方法解析
Nov 11 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数组最大值,最小值的代码
2011/10/31 PHP
详解thinkphp中的volist标签
2018/01/15 PHP
PHP排序算法之快速排序(Quick Sort)及其优化算法详解
2018/04/21 PHP
JavaScript获取多个数组的交集简单实例
2013/11/11 Javascript
浅谈nodeName,nodeValue,nodeType,typeof 的区别
2015/01/13 Javascript
js+div实现文字滚动和图片切换效果代码
2015/08/27 Javascript
一个用jquery写的判断div滚动条到底部的方法【推荐】
2016/04/29 Javascript
JavaScript中的ParseInt("08")和“09”返回0的原因分析及解决办法
2016/05/19 Javascript
AngularJs  unit-testing(单元测试)详解
2016/09/02 Javascript
AngularJS实现树形结构(ztree)菜单示例代码
2016/09/18 Javascript
微信小程序之小豆瓣图书实例
2016/11/30 Javascript
原生JavaScript来实现对dom元素class的操作方法(推荐)
2017/08/16 Javascript
JavaScript模块模式实例详解
2017/10/25 Javascript
React教程之封装一个Portal可复用组件的方法
2018/01/02 Javascript
基于JavaScript实现每日签到打卡轨迹功能
2018/11/29 Javascript
vue悬浮可拖拽悬浮按钮的实例代码
2019/08/20 Javascript
JS变量提升及函数提升实例解析
2020/09/03 Javascript
[14:36]2014 DOTA2国际邀请赛中国区预选赛5.21 Orenda VS NE
2014/05/22 DOTA
Python入门篇之编程习惯与特点
2014/10/17 Python
Python利用ansible分发处理任务
2015/08/04 Python
python+pandas+时间、日期以及时间序列处理方法
2018/07/10 Python
python实现广度优先搜索过程解析
2019/10/19 Python
PyTorch中反卷积的用法详解
2019/12/30 Python
pandas DataFrame运算的实现
2020/06/14 Python
浅谈CSS3动画的回调处理
2016/07/21 HTML / CSS
不可轻视HTML5!App三年内将被html5顶替彻底消失
2015/11/18 HTML / CSS
aden + anais英国官网:美国婴儿贴身用品品牌
2019/09/08 全球购物
Python里面如何拷贝一个对象
2014/02/17 面试题
J2EE的优越性主要表现在哪些方面
2016/03/28 面试题
大学活动总结格式
2014/04/29 职场文书
给校长的建议书300字
2014/05/16 职场文书
2016新教师岗前培训心得体会
2016/01/08 职场文书
2016暑期校本培训心得体会
2016/01/08 职场文书
MySQL 如何分析查询性能
2021/05/12 MySQL
SpringBoot实现quartz定时任务可视化管理功能
2021/08/30 Java/Android
MyBatis核心源码深度剖析SQL语句执行过程
2022/05/20 Java/Android