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中exports与module.exports的区别详细介绍
Jan 14 NodeJs
nodejs教程之环境安装及运行
Nov 21 NodeJs
NodeJS学习笔记之FS文件模块
Jan 13 NodeJs
NodeJs的优势和适合开发的程序
Aug 14 NodeJs
实例分析nodejs模块xml2js解析xml过程中遇到的坑
Mar 18 NodeJs
详解nodejs微信公众号开发——6.自定义菜单
Apr 13 NodeJs
详解NODEJS的http实现
Jan 04 NodeJs
nodejs使用redis作为缓存介质实现的封装缓存类示例
Feb 07 NodeJs
深入理解NodeJS 多进程和集群
Oct 17 NodeJs
nodejs中函数的调用实例详解
Oct 31 NodeJs
Nodejs监控事件循环异常示例详解
Sep 22 NodeJs
NodeJS有难度的面试题(能答对几个)
Oct 09 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
DC的38部超级英雄动画电影
2020/03/03 欧美动漫
php 变量未定义等错误的解决方法
2011/01/12 PHP
Yii查询生成器(Query Builder)用法实例教程
2014/09/04 PHP
php打印输出棋盘的实现方法
2014/12/23 PHP
php生成Android客户端扫描可登录的二维码
2016/05/13 PHP
php生成图片缩略图功能示例
2017/02/22 PHP
jquery操作checked属性以及disabled属性的多种方法
2014/06/20 Javascript
原生javascript实现拖动元素示例代码
2014/09/01 Javascript
jquery.mobile 共同布局遇到的问题小结
2015/02/10 Javascript
jQuery制作仿Mac Lion OS滚动条效果
2015/02/10 Javascript
jQuery实现伪分页的方法分享
2016/02/17 Javascript
javascript ES6 新增了let命令使用介绍
2017/07/07 Javascript
jQuery ajax调用webservice注意事项
2017/10/08 jQuery
详解vue移动端日期选择组件
2018/02/22 Javascript
在Vue项目中使用d3.js的实例代码
2018/05/01 Javascript
详解微信小程序中组件通讯
2018/10/30 Javascript
微信公众号获取用户地理位置并列出附近的门店的示例代码
2019/07/25 Javascript
浅谈JavaScript节流和防抖函数
2020/08/25 Javascript
vue使用transition组件动画效果的实例代码
2021/01/28 Vue.js
[54:08]LGD女子刀塔学院 DOTA2炼金术士教学
2014/01/09 DOTA
Python获取网页上图片下载地址的方法
2015/03/11 Python
Python中格式化format()方法详解
2017/04/01 Python
Django后台获取前端post上传的文件方法
2018/05/28 Python
python实现图片插入文字
2019/11/26 Python
详解Python Opencv和PIL读取图像文件的差别
2019/12/27 Python
python第三方库学习笔记
2020/02/07 Python
keras训练曲线,混淆矩阵,CNN层输出可视化实例
2020/06/15 Python
在线购买世界上最好的酒:BoozeBud
2018/06/07 全球购物
傲盾软件面试题
2015/08/17 面试题
Delphi CS笔试题
2014/01/04 面试题
个人收入证明模板
2014/09/18 职场文书
公司庆典欢迎词
2015/01/26 职场文书
导游词之台湾阿里山
2019/10/23 职场文书
MySQL学习必备条件查询数据
2022/03/25 MySQL
排查MySQL生产环境索引没有效果
2022/04/11 MySQL
Redis keys命令的具体使用
2022/06/05 Redis