美文网首页程序员今日看点
node.js 学习二 之 函数调用

node.js 学习二 之 函数调用

作者: skuare520 | 来源:发表于2016-12-24 11:27 被阅读235次

本地函数

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此访问  
        fun1(response)
        response.end('');
    }
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
//---普通函数      
function fun1(res) {
    console.log("fun1")
    res.write("你好,我是fun1");
}

浏览器:

02_fun1_chrome.png

控制台:

02_fun1_console.png

调用另外一个js文件的函数

1.同级目录下新建文件夹modules,进入文件夹新建一个文件

│  02_funcall.js
└─ modules
        └─otherfuns.js

2.otherfuns.js

function fun2(res) {
    console.log("fun2")
    res.write("hello, 我是fun2")
}

// 如果要声明为可被外部调用的
module.exports = fun2

3.修改原来的02_funcall.js

var http = require('http');
var otherfun = require("./modules/otherfuns.js")

http.createServer(function(request, response) {
    response.writeHead(200, { 'Content-Type': 'text/html;    charset=utf-8' });
    if (request.url !== "/favicon.ico") { //清除第2此访问  
        fun1(response)
        otherfun(response)
        response.end('');
    }
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
//---普通函数      
function fun1(res) {
    console.log("fun1")
    res.write("你好,我是fun1");
}

浏览器:

02_fun2_chrome.png

控制台:

02_fun2_console.png

导出多个函数

otherfuns.js

//支持多个函数      
module.exports = {
    getVisit: function() {
        return visitnum++;
    },
    add: function(a, b) {
        return a + b;
    },
    fun2: function(res) {
        console.log("fun2")
        res.write("hello, 我是fun2")
    },
    fun3: function(res) {
        console.log("fun3")
        res.write("hello, 我是fun3")
    }
}

02_funcall.js

var http = require('http');
var otherfun = require("./modules/otherfuns.js")

http.createServer(function(request, response) {
    response.writeHead(200, { 'Content-Type': 'text/html;    charset=utf-8' });
    if (request.url !== "/favicon.ico") { //清除第2此访问  
        fun1(response)
        otherfun.fun2(response)
        // 用字符串调用对应函数
        otherfun['fun3'](response)
        response.end('');
    }
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
//---普通函数      
function fun1(res) {
    console.log("fun1")
    res.write("你好,我是fun1");
}

相关文章

  • node.js 学习二 之 函数调用

    本地函数 浏览器: 控制台: 调用另外一个js文件的函数 1.同级目录下新建文件夹modules,进入文件夹新建一...

  • 小程序云开发(四)——调用云函数

    云函数: 今天记录一下关于云函数的调用问题,对于云函数的调用,我们首先要在云函数的文件的夹中新建Node.js云函...

  • [转载]JavaScript权威指南(8)--函数

    文章前言 一 函数定义 二 函数调用 1,函数调用 2,方法调用 3,构造函数调用 4,间接调用 三 函数的实参和...

  • 2017年9月20日 学习总结1

    1 今日学习函数的调用和应用。 学习函数的定义及调用,对函数的嵌套调用还不理解;

  • 7、函数

    1、Python之什么是函数 2、Python之调用函数 Python内置了很多有用的函数,我们可以直接调用。 要...

  • Java反射(二)

    类 反射调用一:调用无参构造函数 反射调用二:调用含参构造函数

  • Node.js异常处理

    Node.js异常分类: 变量异常 函数异常 调用异常 变量异常 未定义变量 未包含对象 变量类型错误 函数异常 ...

  • 12.function

    一、定义函数 函数名(){函数体} function 函数名(){函数体} 例如 二、调用函数 无参函数调用方法 ...

  • JavaScript回调函数 setTimeout setInt

    回调函数 前面我们学习的函数中,函数的调用都是主动调用的,即使用函数名加括号来调用。除了主动调用函数外,还有一种机...

  • Python第五堂笔记--函数

    1️⃣、Python之什么是函数 函数就是最基本的一种代码抽象的方式。 2️⃣、Python之调用函数 要调用一个...

网友评论

    本文标题:node.js 学习二 之 函数调用

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