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学习笔记之Http模块
Jan 13 NodeJs
nodejs中的fiber(纤程)库详解
Mar 24 NodeJs
NodeJs读取JSON文件格式化时的注意事项
Sep 25 NodeJs
详谈Angular路由与Nodejs路由的区别
Mar 05 NodeJs
实例分析nodejs模块xml2js解析xml过程中遇到的坑
Mar 18 NodeJs
nodejs 子进程正确的打开方式
Jul 03 NodeJs
nodejs后台集成ueditor富文本编辑器的实例
Jul 11 NodeJs
详解nodeJs文件系统(fs)与流(stream)
Jan 24 NodeJs
Nodejs下使用gm圆形裁剪并合成图片的示例
Feb 22 NodeJs
详解Nodejs内存治理
May 13 NodeJs
linux 下以二进制的方式安装 nodejs
Feb 12 NodeJs
NodeJS开发人员常见五个错误理解
Oct 14 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
十大催泪虐心动漫,你能坚持看到第几部?
2020/03/04 日漫
PHP+DBM的同学录程序(5)
2006/10/09 PHP
基于PHP开发中的安全防范知识详解
2013/06/06 PHP
PHP会话处理的10个函数
2015/08/11 PHP
php进程间通讯实例分析
2016/07/11 PHP
php 输出json及显示json中的中文汉字详解及实例
2016/11/09 PHP
IE innerHTML,outerHTML所引起的问题
2009/06/04 Javascript
jquery 延迟执行实例介绍
2013/08/20 Javascript
node.js解决获取图片真实文件类型的问题
2014/12/20 Javascript
使用mini-define实现前端代码的模块化管理
2014/12/25 Javascript
jQuery中:selected选择器用法实例
2015/01/04 Javascript
js获取内联样式的方法
2015/01/27 Javascript
jquery.qtip提示信息插件用法简单实例
2016/06/17 Javascript
Angular.JS中的指令引用template与指令当做属性详解
2017/03/30 Javascript
js模块加载方式浅析
2017/08/12 Javascript
Vue2.0用户权限控制解决方案
2017/11/29 Javascript
解决vue axios跨域 Request Method: OPTIONS问题(预检请求)
2020/08/14 Javascript
跟老齐学Python之画圈还不简单吗?
2014/09/20 Python
在Python中使用mechanize模块模拟浏览器功能
2015/05/05 Python
异步任务队列Celery在Django中的使用方法
2018/06/07 Python
python for和else语句趣谈
2019/07/02 Python
python3 requests库文件上传与下载实现详解
2019/08/22 Python
如何在python中实现随机选择
2019/11/02 Python
如何分离django中的媒体、静态文件和网页
2019/11/12 Python
Python的几种主动结束程序方式
2019/11/22 Python
Python实现仿射密码的思路详解
2020/04/23 Python
Python 爬虫的原理
2020/07/30 Python
在C#中如何实现多态
2014/07/02 面试题
学生个人的自我评价分享
2013/11/05 职场文书
法学专业自我鉴定
2014/02/05 职场文书
大学生个人求职信
2014/06/02 职场文书
研究生简历自我评
2015/03/11 职场文书
公司宣传语大全
2015/07/13 职场文书
python 用递归实现通用爬虫解析器
2021/04/16 Python
Python+DeOldify实现老照片上色功能
2022/06/21 Python
MySQL中正则表达式(REGEXP)使用详解
2022/07/07 MySQL