首页 > Web开发 > 详细

node.js学习笔记之调用函数

时间:2017-03-10 15:21:54      阅读:150      评论:0      收藏:0      [点我收藏+]

本文件和从其它模块引入函数;

从其他模块引入分只支持引入一个函数和多个  这两种

例:

test.js为主函数

var http=require(‘http‘);
http.createServer(function (request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html‘})
        func1(response);//调用内部
        otherfun.func3(response);//调用外部函数
        console.log(‘访问‘);
        response.end("hello,world\n");

}).listen(8887);
console.log(‘Server runing at http://127.0.0.1:8887‘);
//调用外部函数需引入
var otherfun = require("./moudel/asFunc.js");

var func1=function(res) {
    console.log("func1");
    res.write("hello,woshi func1");
};

asFunc.js

//支持多个函数
module.exports = {
    func2:function(res){
        console.log("func2");
        res.write("func2,,,");
    },
    func3:function(res){
        console.log("func3");
        res.write("func3,,,");
    }
}
// var func2 = function(res){
//     console.log("func2");
//     res.write("func2,,,");
// }
// module.exports = func2; //支持一个函数

执行结果
技术分享

技术分享

 

node.js学习笔记之调用函数

原文:http://www.cnblogs.com/xiaoluoli/p/6530542.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!