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学习笔记之网络编程
Aug 03 NodeJs
轻松创建nodejs服务器(5):事件处理程序
Dec 18 NodeJs
Windows 系统下设置Nodejs NPM全局路径
Apr 26 NodeJs
NodeJS整合银联网关支付(DEMO)
Nov 09 NodeJs
async/await与promise(nodejs中的异步操作问题)
Mar 03 NodeJs
nodejs 图片预览和上传的示例代码
Sep 30 NodeJs
nodejs实现截取上传视频中一帧作为预览图片
Dec 10 NodeJs
详解NODEJS的http实现
Jan 04 NodeJs
详解nodejs通过响应回写的方式渲染页面资源
Apr 07 NodeJs
nodejs 如何手动实现服务器
Aug 20 NodeJs
nodejs中实现修改用户路由功能
May 24 NodeJs
分享五个Node.js开发的优秀实践 
Apr 07 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初学者头疼问题总结
2006/07/08 PHP
《APMServ 5.1.2》使用图解
2006/10/23 PHP
Http 1.1 Etag 与 Last-Modified提高php效率
2008/01/10 PHP
PHP统计二维数组元素个数的方法
2013/11/12 PHP
laravel框架中路由设置,路由参数和路由命名实例分析
2019/11/23 PHP
JS获取客户端IP地址、MAC和主机名的7个方法汇总
2014/07/21 Javascript
JavaScript事件委托实例分析
2015/05/26 Javascript
js仿支付宝多方框输入支付密码效果
2016/09/27 Javascript
Jquery给当前页或者跳转后页面的导航栏添加选中后样式的实例
2016/12/08 Javascript
详解vuex 中的 state 在组件中如何监听
2017/05/23 Javascript
详谈JS中数组的迭代方法和归并方法
2017/08/11 Javascript
Angular实现表单验证功能
2017/11/13 Javascript
在vue中解决提示警告 for循环报错的方法
2018/09/28 Javascript
轻量级富文本编辑器wangEditor结合vue使用方法示例
2018/10/10 Javascript
Node.js如何优雅的封装一个实用函数的npm包的方法
2019/04/29 Javascript
javascript简单实现深浅拷贝过程详解
2019/10/08 Javascript
vue 需求 data中的数据之间的调用操作
2020/08/05 Javascript
javascript中闭包closure的深入讲解
2021/03/03 Javascript
在IIS服务器上以CGI方式运行Python脚本的教程
2015/04/25 Python
Python的Flask框架中使用Flask-SQLAlchemy管理数据库的教程
2016/06/14 Python
python print 按逗号或空格分隔的方法
2018/05/02 Python
Django Rest framework之认证的实现代码
2018/12/17 Python
Python之time模块的时间戳,时间字符串格式化与转换方法(13位时间戳)
2019/08/12 Python
python3调用windows dos命令的例子
2019/08/14 Python
python pygame实现球球大作战
2019/11/25 Python
python如何实现DES加密
2020/09/21 Python
CSS3 实现弹幕的示例代码
2017/08/07 HTML / CSS
保险公司演讲稿
2014/09/02 职场文书
红高粱观后感
2015/06/10 职场文书
MySQL 角色(role)功能介绍
2021/04/24 MySQL
sql中mod()函数取余数的用法
2021/05/29 SQL Server
前端监听websocket消息并实时弹出(实例代码)
2021/11/27 Javascript
alibaba seata服务端具体实现
2022/02/24 Java/Android
P站美图推荐——变身女主角特辑
2022/03/20 日漫
「地球外少年少女」BD发售宣传CM公开
2022/03/21 日漫
使用Redis实现分布式锁的方法
2022/06/16 Redis