码迷,mamicode.com
首页 > Web开发 > 详细

nodeJS调用函数

时间:2017-06-05 10:26:19      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:nodejs 函数 调用 导出

一、调用普通函数

声明函数:

function fun1(res) {
  console.log("fun1");
  res.write("I‘m fun1");
}

在同一文件内调用:

fun1(response);


二、调用其它文件中的函数

声明函数并导出:

function fun2(res) {
  console.log(‘我是fun2‘);
  res.write(‘I‘m fun2‘);
}
module.exports = fun2;

引入模块:

var otherfun = require(‘./models/otherfuns‘);

‘./models/otherfuns.js‘ 表示fun2的所在文件路径

调用函数:

otherfun.fun2(response);


三、导出多个函数

module.exports = {
        fun2:function(res){
		console.log(‘我是fun2‘);
		res.write(‘你好,我是fun2‘);
	},
	fun3:function(res){
		console.log(‘我是fun3‘);
		res.write(‘你好,我是fun3‘);
	}
}


四、用字符串调用对应的函数

otherfun[‘fun2‘](response);
otherfun[‘fun3‘](response);



nodeJS调用函数

标签:nodejs 函数 调用 导出

原文地址:http://11317783.blog.51cto.com/11307783/1932145

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!