美文网首页
Nodejs学习笔记-函数

Nodejs学习笔记-函数

作者: 莫名FCJ | 来源:发表于2017-10-23 16:22 被阅读2次

    调用本地函数
    调用外部函数-支持一个函数
    调用外部函数-支持多个函数
    字符串方式调用函数

    代码:https://github.com/fengchunjian/nodejs_examples/tree/master/funcall

    //vim models/otherfun1.js
    function fun2(res) {
        console.log("我是fun2");
        res.write("hello, fun2\n");
    }
    module.exports = fun2;
    
    //vim models/otherfuns.js
    module.exports = {
        fun3 : function(res) {
            console.log("我是fun3");
            res.write("hello, fun3\n");
        },
        fun4 : function(res) {
            console.log("我是fun4");
            res.write("hello, fun4\n");
        }
    }
    
    //vim funcall.js
    var http = require('http')
    var otherfun1 = require("./models/otherfun1.js");
    var otherfuns = require("./models/otherfuns.js");
    http.createServer(function (request, response) {
        fun1(response);
        otherfun1(response);
        otherfuns.fun3(response);
        otherfuns['fun4'](response);
        response.end();
    }).listen(8000);
    console.log('Server running at http://127.0.0.1:8000/');
    
    function fun1(res) {
        console.log("我是fun1");
        res.write("hello, fun1\n");
    }
    

    node funcall.js
    Server running at http://127.0.0.1:8000/
    我是fun1
    我是fun2
    我是fun3
    我是fun4

    curl http://127.0.0.1:8000/
    hello, fun1
    hello, fun2
    hello, fun3
    hello, fun4

    相关文章

      网友评论

          本文标题:Nodejs学习笔记-函数

          本文链接:https://www.haomeiwen.com/subject/nluauxtx.html