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教程 安装express及配置app.js文件的详细步骤
May 11 NodeJs
nodejs实现黑名单中间件设计
Jun 17 NodeJs
Nodejs极简入门教程(二):定时器
Oct 25 NodeJs
nodejs 的 session 简单使用
Jun 06 NodeJs
Nodejs 搭建简单的Web服务器详解及实例
Nov 30 NodeJs
nodejs中使用HTTP分块响应和定时器示例代码
Mar 19 NodeJs
nodejs个人博客开发第六步 数据分页
Apr 12 NodeJs
nodejs socket实现的服务端和客户端功能示例
Jun 02 NodeJs
详解nodejs通过代理(proxy)发送http请求(request)
Sep 22 NodeJs
nodejs实现截取上传视频中一帧作为预览图片
Dec 10 NodeJs
nodejs简单实现TCP服务器端和客户端的聊天功能示例
Jan 04 NodeJs
nodejs文件夹深层复制功能
Sep 03 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
JavaScript OOP类与继承
2009/11/15 Javascript
jquery的Tooltip插件 qtip使用详细说明
2010/09/08 Javascript
基于jquery的图片的切换(以数字的形式)
2011/02/14 Javascript
对象无length属性时IE6/IE7中无法将其转换成伪数组(ArrayLike)
2011/07/31 Javascript
c#和Javascript操作同一json对象的实现代码
2012/01/17 Javascript
事件冒泡是什么如何用jquery阻止事件冒泡
2013/03/20 Javascript
angularjs 处理多个异步请求方法汇总
2015/01/06 Javascript
jQuery检测某个元素是否存在代码分享
2015/07/09 Javascript
jquery实现的仿天猫侧导航tab切换效果
2015/08/24 Javascript
树结构之JavaScript
2017/01/24 Javascript
jQuery修改DOM结构_动力节点Java学院整理
2017/07/05 jQuery
jquery tmpl模板(实例讲解)
2017/09/02 jQuery
javaScript中"=="和"==="的区别详解
2018/03/16 Javascript
浅谈javascript错误处理
2019/08/11 Javascript
详细解析Python当中的数据类型和变量
2015/04/25 Python
用Python编写生成树状结构的文件目录的脚本的教程
2015/05/04 Python
PHP实现发送和接收JSON请求
2018/06/07 Python
Python3按一定数据位数格式处理bin文件的方法
2019/01/24 Python
Python os.access()用法实例
2019/02/18 Python
Python使用sax模块解析XML文件示例
2019/04/04 Python
python实现五子棋游戏
2019/06/18 Python
Python类super()及私有属性原理解析
2020/06/15 Python
基于Python正确读取资源文件
2020/09/14 Python
html5应用缓存_动力节点Java学院整理
2017/07/13 HTML / CSS
波兰最大的儿童服装连锁店之一:5.10.15.
2018/02/11 全球购物
沙特阿拉伯网上购物:Sayidaty Mall
2018/05/06 全球购物
精通CAD能手自荐书
2014/01/31 职场文书
高二政治教学反思
2014/02/01 职场文书
军训教官感言
2014/03/02 职场文书
煤矿安全承诺书
2014/05/22 职场文书
一年级班主任工作总结2014
2014/11/08 职场文书
2016年教代会开幕词
2016/03/04 职场文书
普希金诗歌赏析(6首)
2019/08/22 职场文书
用golang如何替换某个文件中的字符串
2021/04/25 Golang
python中的装饰器该如何使用
2021/06/18 Python
Python内置的数据类型及使用方法
2022/04/13 Python