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实战心得之eventproxy模块控制并发
Oct 27 NodeJs
进阶之初探nodeJS
Jan 24 NodeJs
Nodejs 发送Post请求功能(发短信验证码例子)
Feb 09 NodeJs
Windows下快速搭建NodeJS本地服务器的步骤
Aug 09 NodeJs
nodejs创建简易web服务器与文件读写的实例
Sep 07 NodeJs
nodejs对express中next函数的一些理解
Sep 08 NodeJs
详解nodejs中express搭建权限管理系统
Sep 15 NodeJs
nodejs操作mongodb的增删改查功能实例
Nov 09 NodeJs
NodeJS爬虫实例之糗事百科
Dec 14 NodeJs
NodeJS实现不可逆加密与密码密文保存的方法
Mar 16 NodeJs
nodejs取得当前执行路径的方法
May 13 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
php检测图片木马多进制编程实践
2013/04/11 PHP
Yii2隐藏frontend/web和backend/web的方法
2015/12/12 PHP
理解PHP中的Session及对Session有效期的控制
2016/01/08 PHP
PHP实现判断数组是一维、二维或几维的方法
2017/02/06 PHP
PHP基于redis计数器类定义与用法示例
2018/02/08 PHP
php设计模式之建造器模式分析【星际争霸游戏案例】
2020/01/23 PHP
JavaScript类和继承 constructor属性
2010/03/04 Javascript
JS实现QQ图片一闪一闪的效果小例子
2013/07/31 Javascript
通过JS获取Request.QueryString()参数的值实现方法
2016/09/27 Javascript
AngularJS过滤器filter用法实例分析
2016/11/04 Javascript
jquery中有哪些api jQuery主要API
2017/11/20 jQuery
简述vue状态管理模式之vuex
2018/08/29 Javascript
Angular利用HTTP POST下载流文件的步骤记录
2020/07/26 Javascript
python爬取微信公众号文章的方法
2019/02/26 Python
简单了解python PEP的一些知识
2019/07/13 Python
python 串口读取+存储+输出处理实例
2019/12/26 Python
tensorflow之自定义神经网络层实例
2020/02/07 Python
使用Python实现牛顿法求极值
2020/02/10 Python
安装完Python包然后找不到模块的解决步骤
2020/02/13 Python
keras绘制acc和loss曲线图实例
2020/06/15 Python
馥蕾诗美国官网:Fresh美国
2019/10/09 全球购物
应届行政管理专业个人自我评价
2013/12/28 职场文书
顶岗实习接收函
2014/01/09 职场文书
运动会广播稿60字
2014/01/15 职场文书
教师个人自我鉴定
2014/02/08 职场文书
公司保密承诺书
2014/03/27 职场文书
护理专业自荐书
2014/06/04 职场文书
小学优秀学生评语
2014/12/29 职场文书
2015年感恩母亲节活动方案
2015/05/04 职场文书
关于法制教育的宣传语
2015/07/13 职场文书
2015年财务人员个人工作总结
2015/07/27 职场文书
小学语文教学随笔
2015/08/14 职场文书
2016优秀教师先进个人事迹材料
2016/02/25 职场文书
《鲁滨逊漂流记》之六读后感(4篇)
2019/09/29 职场文书
Python 如何安装Selenium
2021/05/06 Python
Python 实现绘制子图及子图刻度的变换等问题
2021/05/31 Python