node.js中的http+https模块

http和https:

http协议:http客服端发起请求,创建端口,http服务器在端口监听客户端的请求,http服务器向端口返回状态和内容的一个过程。

https协议:是在http协议上增加了SSL/TLS握手加密传输,当访问该协议时,需要SSL证书,其他的应用与http基本一致。

无论是http还是https请求方式都是:get、post、put(更新)

一、http的事件连接:

1、所有node.js中的模块在使用前都需将其引用到所需要的脚本内部,然后在对其操作,http也是一样的,

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此访问  
        console.log('访问');  
        response.write('hello,world');  
        response.end('hell,世界');//不写则没有http协议尾,但写了会产生两次访问  
    }  
}).listen(8000);  
console.log('Server  running  at  http://127.0.0.1:8000/'); 

2、http.createServer,是http监听函数方法,里面会调用一个回调函数,会有两个参数,request(请求)、response(响应)

3、listen(端口号):此处的端口号可以随意

4、在创建服务器时,需要将回复给一个end协议尾,如果不加response.end()方法,浏览器会一直在转圈,感觉像是在一直加载的样子,如果加入每次访问此服务器时会同时访问两次,所以就需要再加入response.end()方法前先进行判断request.url!=="/favicon.ico",如果不等于才继续走下去,注意:在express等架构中已经将其封装好,无需再判断,使用时自行判断

二、脚本间的引用(require、exports、module.exports)

1、test.js脚本中的代码

var http = require('http');
var test1= require("./models/test1.js");
var test2= require('./models/test2');
http.createServer(function (request, response) {
  response.writeHead(200, { 'Content-Type': 'text/html;        charset=utf-8' });
  if (request.url !== "/favicon.ico") {        //清除第2此访问
    //-------用字符串调用对应的函数---
    funname = 'fun3';
    test1[funname](response);
    test1['fun2'](response);

    //引入test2中的返回函数
    test2=new Teacher(1,'李四',30);
    test2.enter();
    test2.teach(res);
    res.end(''); 
    response.end('');
  }
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');

2、在test脚本的同级建一个models目录里面建一个test1.js脚本,代码如下

//  只支持一个函数  
function Teacher(id,name,age){
    User.apply(this,[id,name,age]);
    this.teach=function(res){
        res.write(this.name+"老师讲课");
    }
}
module.exports = Teacher;
 
  
//使用对象的格式输出多个函数      
module.exports={      
    fun2:function(res){      
        res.write('fun二');
    },      
    fun3:function(res){      
        res.write('fun三');
    }      
}   

3、在test脚本的同级建一个models目录里面建一个test2.js脚本,代码如下

var  User  =  require('./User');
function test2(id,name,age){
//应用apply继承User所有的方法
    User.apply(this,[id,name,age]);
    this.teach=function(res){
        res.write(this.name+"老师讲课");
    }
}
module.exports = test2;

4、在test脚本的同级建一个models目录里面建一个Userjs脚本,代码如下

function  User(id,name,age){
    this.id=id;
    this.name=name;
    this.age=age;
    this.enter=function(){
        console.log(name+"进入图书馆");
    }
}
module.exports=User;

5、运行node test.js (如果是用vscode编译器可直接ctrl+~快捷键启动控制台、gitbashhere启动当前目录下的test脚本、或者在当前脚本文件夹中按住shift+鼠标右键也可启动Powershell窗口进行启动服务器),打开浏览器输入http://localhost:8000/进行访问此服务器,网页上输出 fun二 fun三 李四老师讲课

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值