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开发微信公众号后台服务实例
Sep 03 NodeJs
NodeJs的优势和适合开发的程序
Aug 14 NodeJs
学习 NodeJS 第八天:Socket 通讯实例
Dec 21 NodeJs
详解nodejs通过代理(proxy)发送http请求(request)
Sep 22 NodeJs
nodejs+mongodb+vue前后台配置ueditor的示例代码
Jan 02 NodeJs
Nodejs连接mysql并实现增、删、改、查操作的方法详解
Jan 04 NodeJs
nodejs中Express与Koa2对比分析
Feb 06 NodeJs
nodejs连接mysql数据库及基本知识点详解
Mar 20 NodeJs
详解Nodejs get获取远程服务器接口数据
Mar 26 NodeJs
纯异步nodejs文件夹(目录)复制功能
Sep 03 NodeJs
nodejs实现百度舆情接口应用示例
Feb 07 NodeJs
通过实例了解Nodejs模块系统及require机制
Jul 16 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 验证码制作(网树注释思想)
2009/07/20 PHP
thinkphp缓存技术详解
2014/12/09 PHP
PHP中迭代器的简单实现及Yii框架中的迭代器实现方法示例
2020/04/26 PHP
JavaScript-世界上误解最深的语言分析
2007/08/12 Javascript
javascript 解析url的search方法
2010/02/09 Javascript
Node.js和PHP根据ip获取地理位置的方法
2014/03/14 Javascript
javascript数组排序汇总
2015/07/07 Javascript
js 判断所选时间(或者当前时间)是否在某一时间段的实现代码
2015/09/05 Javascript
jQuery实现的个性化返回底部与返回顶部特效代码
2015/10/30 Javascript
Javascript中的async awai的用法
2017/05/17 Javascript
JavaScript字符串_动力节点Java学院整理
2017/06/27 Javascript
angular2模块和共享模块详解
2018/04/08 Javascript
JavaScript对象的特性与实践应用深入详解
2018/12/30 Javascript
在vue中使用el-tab-pane v-show/v-if无效的解决
2020/08/03 Javascript
antd 表格列宽自适应方法以及错误处理操作
2020/10/27 Javascript
jQuery列表动态增加和删除的实现方法
2020/11/05 jQuery
[00:55]2015国际邀请赛中国区预选赛5月23日——28日约战上海
2015/05/25 DOTA
go语言计算两个时间的时间差方法
2015/03/13 Python
Python网络爬虫出现乱码问题的解决方法
2017/01/05 Python
python运行其他程序的实现方法
2017/07/14 Python
Python获取二维矩阵每列最大值的方法
2018/04/03 Python
python中找出numpy array数组的最值及其索引方法
2018/04/17 Python
Python调用C++,通过Pybind11制作Python接口
2018/10/16 Python
解决Python 命令行执行脚本时,提示导入的包找不到的问题
2019/01/19 Python
django 使用全局搜索功能的实例详解
2019/07/18 Python
基于Python实现签到脚本过程解析
2019/10/25 Python
如何解决tensorflow恢复模型的特定值时出错
2020/02/06 Python
pyqt5 textEdit、lineEdit操作的示例代码
2020/08/12 Python
CSS3 实现侧边栏展开收起动画
2014/12/22 HTML / CSS
外国人聘用意向书
2014/04/01 职场文书
学校读书活动总结
2014/06/30 职场文书
公司员工安全协议书
2014/11/21 职场文书
2019年特色火锅店的创业计划书模板
2019/08/28 职场文书
Python爬虫之爬取最新更新的小说网站
2021/05/06 Python
总结几个非常实用的Python库
2021/06/26 Python
详解nginx location指令
2022/01/18 Servers