node.js学习之调用函数
来源:互联网 发布:3m平台 源码 编辑:程序博客网 时间:2024/06/05 23:11
//1、在本文件内调用函数/* var http = require('http');http.createServer(function(request,response){response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'}); if(request.url!=="/favicon.ico"){//调用函数fun1函数fun1(response);response.end('');}}).listen(3000);console.log('Server running at http://127.0.0.1:3000/'); //普通函数function fun1(res){res.write("Hello, I am fun1");} */
打开命令行输入
>node n2_funcall.js
输出结果:
n2_funcall.js
//2、可以在调用其它文件夹下的函数var http = require('http');//导入在当前目录文件夹models下的otherfuns.js文件var otherFun = require('./models/otherfuns.js');http.createServer(function(request,response){response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'}); if(request.url!=="/favicon.ico"){otherFun(request,response);response.end('');}}).listen(3000);console.log('Server running at http://127.0.0.1:3000/');
./models/otherfuns.js
function fun1(req,res){res.write('我是fun1函数');//call('hello',req,res);res.write('');}module.exports = fun1;//只支持一个函数运行结果:
3、调用其它文件中的多个函数
n2_funcall.js
//3、调用其它文件夹下的多个函数var http = require('http');var otherFun = require('./models/otherfuns.js');http.createServer(function(request,response){response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'}); if(request.url!=="/favicon.ico"){//otherFun.fun2(response);//还可以像下面这样调用fun2函数otherFun['fun2'](response);otherFun.fun3(response);}}).listen(3000);console.log('Server running at http://127.0.0.1:3000/');models/otherfuns.js
module.exports={fun2:function(res){res.write('我是fun2函数');},fun3:function(res){res.write('我是fun3函数');}}
运行结果:
阅读全文
0 0
- node.js学习之调用函数
- node.js学习之module调用方式
- node.js学习之调用模块
- Node.js调用函数
- node.js之学习
- node.js学习之node的安装
- node.js 调用 mongoose 的 aggregate 函数
- node.js学习之markdown
- JS学习之Node类型
- Node.js之学习网站
- 学习node.js之stream
- Node.js学习之Telnet
- Node.js学习笔记_模块调用
- node.js 调用 C++ 学习资源
- Node.js学习教程之二:node.js的HelloWorld
- 【node.js学习之路】什么是Node.js?
- node.js认识学习四:node之初之体验
- NODE.JS async异步调用之串行化调用
- 对DC中parallel_case的理解
- Linux 开启防火墙和开启指定端口命令
- 回文数
- Android Butterknife 8.4.0 使用方法总结
- 第四章例子程序
- node.js学习之调用函数
- 使用引用返回函数值
- NOI2016搞基记
- 希望能够坚持下去每一天
- Linux+Nginx+PHP+MYSQL主流架构实战(LNMP)
- Linux学习高级篇(四)
- NO.2 树的同构
- idea14导入eclipse项目并部署运行完整步骤
- 用Redis Desktop Manager连接Redis(CentOS)