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命令行参数处理模块commander使用实例
Sep 17 NodeJs
实例详解Nodejs 保存 payload 发送过来的文件
Jan 14 NodeJs
浅析Nodejs npm常用命令
Jun 14 NodeJs
Nodejs实现短信验证码功能
Feb 09 NodeJs
浅析 NodeJs 的几种文件路径
Jun 07 NodeJs
使用Nodejs连接mongodb数据库的实现代码
Aug 21 NodeJs
Windows下使用Nodejs运行js的方法
Sep 02 NodeJs
nodejs基于express实现文件上传的方法
Mar 19 NodeJs
nodejs搭建本地服务器并访问文件操作示例
May 11 NodeJs
nodejs实现用户登录路由功能
May 22 NodeJs
如何让Nodejs支持H5 History模式(connect-history-api-fallback源码分析)
May 30 NodeJs
nodejs nedb 封装库与使用方法示例
Feb 06 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把JPEG图片转换成Progressive JPEG的方法
2014/06/30 PHP
PHP的反射类ReflectionClass、ReflectionMethod使用实例
2014/08/05 PHP
PHP生成(支持多模板)二维码海报代码
2018/04/30 PHP
js的Boolean对象初始值示例
2014/03/04 Javascript
js 显示base64编码的二进制流网页图片
2014/04/04 Javascript
jquery+php实现搜索框自动提示
2014/11/28 Javascript
javascript显示中文日期的方法
2015/06/18 Javascript
Javascript实现商品秒杀倒计时(时间与服务器时间同步)
2015/09/16 Javascript
JQUERY表单暂存功能插件分享
2016/02/23 Javascript
jQuery获取复选框被选中数量及判断选择值的方法详解
2016/05/25 Javascript
详解ES6之用let声明变量以及let loop机制
2017/07/15 Javascript
关于前后端json数据的发送与接收详解
2017/07/30 Javascript
Node.js 的模块知识汇总
2017/08/16 Javascript
bootstrap实现二级下拉菜单效果
2017/11/23 Javascript
node.js连接mysql与基本用法示例
2019/01/05 Javascript
Vue在 Nuxt.js 中重定向 404 页面的方法
2019/04/23 Javascript
微信小程序身份证验证方法实现详解
2019/06/28 Javascript
vue 实现走马灯效果
2019/10/28 Javascript
JavaScript ECMA-262-3 深入解析(二):变量对象实例详解
2020/04/25 Javascript
[04:17]DOTA2完美盛典,rOtk、BurNIng携手巴图演唱《倔强》
2017/11/28 DOTA
解读! Python在人工智能中的作用
2017/11/14 Python
numpy实现合并多维矩阵、list的扩展方法
2018/05/08 Python
Python for循环生成列表的实例
2018/06/15 Python
python 将大文件切分为多个小文件的实例
2019/01/14 Python
浅谈python3中input输入的使用
2019/08/02 Python
python系列 文件操作的代码
2019/10/06 Python
python 实现turtle画图并导出图片格式的文件
2019/12/07 Python
Python使用正则表达式实现爬虫数据抽取
2020/08/17 Python
python多线程爬取西刺代理的示例代码
2021/01/30 Python
Aveda美国官网:天然护发产品、洗发水、护发素和沙龙
2016/12/09 全球购物
秘书岗位职责
2013/11/18 职场文书
电子专业推荐信范文
2013/11/18 职场文书
英语故事演讲稿
2014/04/29 职场文书
教师节老师寄语
2015/05/28 职场文书
2019年年中职场激励人心语录30条
2019/08/07 职场文书
详解php中流行的rpc框架
2021/05/29 PHP