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 相关文章推荐
Ubuntu中搭建Nodejs开发环境过程分享
Jun 01 NodeJs
基于NodeJS的前后端分离的思考与实践(四)安全问题解决方案
Sep 26 NodeJs
NodeJS学习笔记之Connect中间件模块(一)
Jan 27 NodeJs
nodejs 整合kindEditor实现图片上传
Feb 03 NodeJs
nodejs创建简易web服务器与文件读写的实例
Sep 07 NodeJs
nodejs实现OAuth2.0授权服务认证
Dec 27 NodeJs
修改Nodejs内置的npm默认配置路径方法
May 13 NodeJs
Nodejs核心模块之net和http的使用详解
Apr 02 NodeJs
nodejs分离html文件里面的js和css的方法
Apr 09 NodeJs
nodejs中request库使用HTTPS代理的方法
Apr 30 NodeJs
nodejs读取图片返回给浏览器显示
Jul 25 NodeJs
nodejs的安装使用与npm的介绍
Sep 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的curl开启问题探讨
2014/04/08 PHP
php中多维数组按指定value排序的实现代码
2014/08/19 PHP
ThinkPHP5.1的权限控制怎么写?分享一个AUTH权限控制
2021/03/09 PHP
使用PHP+JQuery+Ajax分页的实现
2013/04/23 Javascript
js类定义函数时用prototype与不用的区别示例介绍
2014/06/10 Javascript
用javascript将数据导入Excel示例代码
2014/09/10 Javascript
js实现点击图片改变页面背景图的方法
2015/02/28 Javascript
JQuery判断radio(单选框)是否选中和获取选中值方法总结
2015/04/15 Javascript
简介JavaScript中strike()方法的使用
2015/06/08 Javascript
JS实现左右拖动改变内容显示区域大小的方法
2015/10/13 Javascript
跟我学习javascript的prototype,getPrototypeOf和__proto__
2015/11/17 Javascript
js简单倒计时实现代码
2016/04/30 Javascript
关于ES6的六个小特性(二)
2017/02/20 Javascript
基于Vue+elementUI实现动态表单的校验功能(根据条件动态切换校验格式)
2019/04/04 Javascript
如何使用JavaScript实现无缝滚动自动播放轮播图效果
2020/08/20 Javascript
[50:29]2014 DOTA2华西杯精英邀请赛 5 24 DK VS iG
2014/05/26 DOTA
[04:44]显微镜下的DOTA2第二期——你所没有注意到的细节
2014/06/20 DOTA
[34:08]2018DOTA2亚洲邀请赛3月29日 小组赛B组 VP VS EG
2018/03/30 DOTA
python使用百度翻译进行中翻英示例
2014/04/14 Python
python requests 使用快速入门
2017/08/31 Python
python pexpect ssh 远程登录服务器的方法
2019/02/14 Python
Django框架验证码用法实例分析
2019/05/10 Python
python支付宝支付示例详解
2019/08/22 Python
QML实现钟表效果
2020/06/02 Python
在tensorflow下利用plt画论文中loss,acc等曲线图实例
2020/06/15 Python
HTML5中实现拖放效果无须借助javascript
2012/12/26 HTML / CSS
戴尔英国翻新电脑和电子产品:Dell UK Refurbished Computers
2019/07/30 全球购物
介绍一下Java中标识符的命名规则
2014/02/03 面试题
物流仓储计划书
2014/01/10 职场文书
学用政策心得体会
2014/09/10 职场文书
2014村书记党建工作汇报材料
2014/11/02 职场文书
组织生活会发言材料
2014/12/15 职场文书
发布会邀请函
2015/01/31 职场文书
2015大学迎新晚会主持词
2015/07/16 职场文书
导游词之蜀山胜景瓦屋山
2019/11/29 职场文书
Python数据分析之pandas读取数据
2021/06/02 Python