nodejs学习笔记三 调用函数
来源:互联网 发布:武术机器人 算法 编辑:程序博客网 时间:2024/06/17 14:57
一、调用本地函数
调用本地的函数可以直接写一个函数,然后直接调用就行了,和其他语言类似。
var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'}); if(request.url!=="/favicon.ico"){ //清除第2此访问 function1(response); response.end('hell,世界');//不写则没有http协议尾,但写了会产生两次访问 } }).listen(8080); console.log('Server running at http://127.0.0.1:8080/'); function function1(res){ console.log('我是function1'); res.write('我是function1'); }
二、调用外部函数
可以再文件中写入多个函数:
第一种方式调用:
a.js
var http = require('http'); var otherfun = require('./modue.js'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'}); if(request.url!=="/favicon.ico"){ //清除第2此访问 otherfun.function2(response);otherfun.function3(response); response.end('hell,世界');//不写则没有http协议尾,但写了会产生两次访问 } }).listen(8080); console.log('Server running at http://127.0.0.1:8080/');
modue.js
module.exports={ function2:function(res){ console.log("我是function2"); res.write("我是function2"); }, function3:function(res){ console.log("我是function3"); res.write("我是function3"); }}
第二种方调用函数:
var functionname="function2"; 定义一个字符串类型,存放函数的名称,
otherfun[functionname](response); 再通过字符串调用这个,函数,以后会经常用到
阅读全文
0 0
- nodejs学习笔记三 调用函数
- Nodejs学习笔记(三)
- 学习nodejs第三课,调用函数
- Nodejs笔记(三)
- nodejs学习三
- 【c和指针学习笔记】函数指针的初始化以及函数调用的三种方式
- 【Python学习笔记】调用函数
- js学习笔记-函数调用
- Python学习笔记三:函数
- python学习笔记(三)函数
- nodejs学习笔记三——nodejs使用富文本插件ueditor
- nodejs学习笔记(三)——javascript的回调与nodejs的事件循环
- nodejs 学习笔记--debug
- NodeJS学习笔记
- nodejs学习笔记
- nodeJs学习笔记
- nodejs学习笔记
- NodeJS 学习笔记1
- jdk安装 环境变量配置
- 网络及TCP/IP简明快速基础
- 通谷易懂区块链技术
- kafka安装及配置文件详解
- 时间处理
- nodejs学习笔记三 调用函数
- 2.7.2hadoop分布式安装
- maven排除groupId所有下的包
- androidstudio上传svn服务器及问题解决
- Python-function
- google Guava缓存
- Markdown编辑器 使用快速入门<全>
- 计算机视觉、机器学习、模式识别、图像处理领域国内外期刊、会议汇总
- EF报 错误:已授予对“System, PublicKey=00000000000000000400000000000000”的友元访问的错误...