nodejs进阶2--函数模块调用

函数调用

1. 文件内普通函数调用

创建一个js文件命名为2_callFunction.js,其中定义一个函数fun1,向返回对象输出了一段字符串“你好,我是fun1”。

 1 //--------------------2_callFunction.js---------------------------------    
 2 var  http = require(‘http‘);       
 3 http.createServer(function    (request,    response)    {      
 4     response.writeHead(200,    {‘Content-Type‘:    ‘text/html;    charset=utf-8‘});      
 5     if(request.url!=="/favicon.ico"){ //清除第2此访问  
 6         fun1(response);//调用普通函数fun1
 7         response.end();    
 8     }  
 9 }).listen(8000);      
10 console.log(‘Server running at http://127.0.0.1:8000/‘);    
11 //---普通函数      
12 function  fun1(res){      
13     res.write("你好,我是fun1");      
14 }      

我们运行:node 2_callFunction,打开浏览器

2. 调用其他文件里的函数

  首先我们先创建一个otherfuns.js文件,这个文件里有两个函数,call函数被controller调用,这两个函数都向response对象输出一段字符串,函数通过module.exports提供给外部调用。这里我们只提供对外一个函数controller

 1 //-------------------models/otherfuns.js--------------------------      
 2 function  controller(res){      
 3     res.write("执行controller <br>");      
 4     call(res);      
 5     res.end();      
 6 }      
 7 function  call(res){      
 8     res.write("执行call");      
 9 }      
10 module.exports  =  controller;    //只支持一个函数      

我们通过require将otherfuns.js引入到主文件里,require工作机制参见 require() 源码解读

下面两句都可以调用到controller函数:

1) var other =new otherfun (response);//otherfuns.js 里的方法controller被调用
 2) otherfun (response);//otherfuns.js 里的方法controller被调用

 1 //--------------------2_callFunction.js---------------------------------  调用otherfuns.js里的函数
 2 var  http = require(‘http‘);    
 3 var  otherfun = require(‘./models/otherfuns.js‘);         
 4 http.createServer(function    (request,    response)    {      
 5     response.writeHead(200,    {‘Content-Type‘:    ‘text/html;    charset=utf-8‘});      
 6     if(request.url!=="/favicon.ico"){ //清除第2此访问  
 7         var other =new otherfun (response);//otherfuns.js 里的方法controller被调用
 8         //otherfun (response);//otherfuns.js 里的方法controller被调用
 9         response.end();    
10     }  
11 }).listen(8000);      
12 console.log(‘Server running at http://127.0.0.1:8000/‘);  

我们运行:node 2_callFunction,打开浏览器

3. 提供调用多个函数的写法:

第一种:

 1 //支持多个函数    
 2 function  controller(res){      
 3     res.write("执行controller <br>");           
 4     res.end();      
 5 }      
 6 function  call(res){      
 7     res.write("执行call");  
 8     res.end();          
 9 }      
10 module.exports.controller =  controller;
11 module.exports.call =call;

第二种:

 1 //支持多个函数      
 2 module.exports={      
 3     controller:function(res){      
 4         res.write("执行controller <br>");      
 5         res.end();    
 6     },      
 7     call:function(res){      
 8         res.write("执行call <br>");      
 9         res.end();    
10     }      
11 } 

调用方式相比只支持一个函数的方式,需要修改成如下调用方式

otherfun.controller(response);//otherfuns.js 里的函数controller被调用

4. 模块化调用应用(面向对象)

我们建立一个User对象

 1 //--------------User.js--------------  
 2 function  User(id,name,age){
 3     this.id=id;//属性
 4     this.name=name;//属性
 5     this.age=age;//属性
 6     this.enter=function(){//方法
 7         console.log("进入图书馆");
 8     }
 9 }
10 module.exports  =  User;

再建一个Teacher对象

1 //-------------------models/Teacher.js---------  
2 var  User  =  require(‘./User‘);
3 function  Teacher(id,name,age){
4     User.apply(this,[id,name,age]);//继承User
5     this.teach=function(res){//自有方法
6         res.write(this.name+"老师讲课");
7     }
8 }
9 module.exports    =    Teacher; 

Teacher继承User对象,有id,name,age属性,除了enter方法外还定义了teach方法。

apply可以执行多次,所以可以继承多个对象,不如其他语言的面向对象更加严格。

在server端可以如下调用teacher。Teacher(1,‘李四‘,30),初始化了一个实例对象

 1 //----------------------n3_modalcall.js-------------  
 2 var http = require(‘http‘);    
 3 var  Teacher  =  require(‘./models/Teacher‘);
 4 http.createServer(function (request, response)        {        
 5         response.writeHead(200, {‘Content-Type‘: ‘text/html;  charset=utf-8‘});        
 6         if(request.url!=="/favicon.ico"){        //清除第2此访问
 7           teacher  =  new  Teacher(1,‘李四‘,30);
 8           teacher.teach(response);
 9           response.end(‘‘);    
10     }
11 }).listen(8000);        
12 console.log(‘Server running  at  http://127.0.0.1:8000/‘);

我们运行:node 3_modelCall,打开浏览器

时间: 2024-08-03 13:21:39

nodejs进阶2--函数模块调用的相关文章

Nodejs进阶:核心模块net入门与实例讲解

模块概览 net模块是同样是nodejs的核心模块.在http模块概览里提到,http.Server继承了net.Server,此外,http客户端与http服务端的通信均依赖于socket(net.Socket).也就是说,做node服务端编程,net基本是绕不开的一个模块. 从组成来看,net模块主要包含两部分,了解socket编程的同学应该比较熟悉了: net.Server:TCP server,内部通过socket来实现与客户端的通信. net.Socket:tcp/本地 socket的

Nodejs进阶:crypto模块中你需要掌握的安全基础知识

一. 文章概述 互联网时代,网络上的数据量每天都在以惊人的速度增长.同时,各类网络安全问题层出不穷.在信息安全重要性日益凸显的今天,作为一名开发者,需要加强对安全的认识,并通过技术手段增强服务的安全性. crypto模块是nodejs的核心模块之一,它提供了安全相关的功能,如摘要运算.加密.电子签名等.很多初学者对着长长的API列表,不知如何上手,因此它背后涉及了大量安全领域的知识. 本文重点讲解API背后的理论知识,主要包括如下内容: 摘要(hash).基于摘要的消息验证码(HMAC) 对称加

nodejs进阶3-路由处理

1. 路由选择过程 处理不同的HTTP请求在我们的代码中是一个不同的部分,叫做“路由选择”. 那么,我们接下来就创造一个叫做 路由 的模块吧.我们需要查看HTTP请求,从中提取出请求的URL以及GET/POST参数. url.parse(string).query | url.parse(string).pathname | | | | | ------ ------------------- http://localhost:8888/start?foo=bar&hello=world ---

cocos进阶教程(1)Lua调用自定义C++类和函数的最佳实践

第一层:纯C环境下,把C函数注册进Lua环境 a.lua 文件 print(foo(99)) a.c 文件 #include <lua.h> #include <lualib.h> #include <lauxlib.h> int foo(lua_State *L) { int n = lua_tonumber(L, 1); lua_pushnumber(L, n + 1); return 1; } int main() { lua_State *L = lua_ope

【VS开发】MFC中调用C函数模块的解决方案

[VS开发]MFC中调用C函数模块的解决方案 标签(空格分隔): [VS开发] 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明:最近调试基于MFC的程序,当通过外部C文件引入某个function的时候,又一次忘记了C文件与C++文件的区别,直接按照一般的方式,将函数声明放入头文件,将函数定义放入C文件,然后再MFC中通过包含头文件来引用对应的function,然而爆出了链接时无法定位的错误,本文就来梳理一下C++中调用C中功能函数的方法. 首先在C

[转]nodejs中的process模块--child_process.exec

1.process是一个全局进程,你可以直接通过process变量直接访问它. process实现了EventEmitter接口,exit方法会在当进程退出的时候执行.因为进程退出之后将不再执行事件循环,所有只有那些没有回调函数的代码才会被执行. 在下面例子中,setTimeout里面的语句是没有办法执行到的. 1 process.on('exit', function () { 2 setTimeout(function () { 3 console.log('This will not ru

nodejs进阶(5)—接收参数

1. get请求参数接收 我们简单举一个需要接收参数的例子 如果有个查找功能,查找关键词需要从url里接收,http://localhost:8000/search?keyword=地球.通过前面的进阶3教程<nodejs进阶(3)—路由处理>重介绍的url模块,我们知道接收方法如下这样写 1 //----------------6_param------------------------------------       2 var http = require('http');    

介绍nodejs中的path模块的几个方法

webpack中常用的: var path = require('path') 是nodejs中的path模块,介绍一下webpack中常用的几个path模块的方法: 应用node环境的时候,这个path模块的方法经常被用到,处理路径的方法. Nodejs的path模块介绍: 网址:http://nodejs.cn/api/path.html path 模块提供了一些工具函数,用于处理文件与目录的路径.可以通过以下方式使用: const path = require('path'); 或 var

C语言函数嵌套调用作业

一.实验作业 1.1 PTA题目:6-4 十进制转换二进制 设计思路 如果n大于1 对n/2继续进行该函数运算 输出n%2的值 代码截图 调试问题 我第一次做的时候判断的边界条件是大于0继续进行运算,但这样是错的,因为当n等于1时,n%2等于0,由于二进制是逆序输出,又因为这个0是放在开头的,可省略 1.2 学生成绩管理系统 1.2.1 画函数模块图,简要介绍函数功能. 1.2.2 截图展示你的工程文件 1.2.3 函数代码部分截图 主函数 新建 修改 输出全部 1.2.4 调试结果展示 新建与