详细说明了net和http在Nodejs核心模块中的使用
前言
Net和http模块是node的核心模块之一,它们可以构建自己的服务器和客户端来响应和发送请求。
Net模块服务器/客户端
这里编写的网络模块是一个基于tcp协议的服务器和客户端,通过net.createServer和net.connect实现了一个简单的请求和响应演示.
//TCP server varnet=require(' net ')var sever=net . createserver(Function(connection){//客户端关闭连接的事件执行connection.on('end ',Function(){//console . log(' client close connection ')})connection . on(' data ',Function(data){ console . log(' server:客户端发送的数据为' data . tostring())})//data connection . write(' responseHello ')})sever . listen(8080,Function(){//console . log(' listing 函数(data){ console.log ('client:从服务器接收的数据是' data . tostring())client . end()})//将数据传递给服务器client . write(' hello ')client . on(' end ',function () {//console.log('断开与服务器的连接。
http模块四种请求类型
Http服务器:
Http.createServer创建一个Http实例。服务器,它使用一个函数作为http请求处理函数。该函数接受两个参数,即请求对象(req)处理请求的信息和响应对象(res)处理响应的数据。
//http server const http=require(' http ');var fs=require(' fs ');var URL=require(' URL ')http . createserver(function(req,RES){ var URL path=URL . parse(req . URL);Varmeth=req。方法//urlpath。pathname获取并设置URL的路径部分//meth获取请求数据的方法,一个路径只能被一个方法请求,其他方法返回404if (urlpath。pathname=='/' meth==' get '){ RES . write(')} else if(urlpath . pathname=='/users ' meth==' POST '){ RES . write head(200,{ ' content-type ' : ' text/html;charset=utf-8 ' });fs.readFile('user.json ',函数(err,data){ if(err){ return console . error(err);} var data=data . tostring();//返回数据res.write(数据);});} else if(urlpath . pathname=='/list ' meth==' PUT '){ RES . write(' PUT ok ');} else if(urlpath . pathname=='/detail ' meth==' DELETE '){ RES . write(' DELETE ok ');} else { res.writeHead(404,{ ' content-type ' : ' text/html;charset=utf-8 ' });res.write('404') } res.on('data ',function(data){ console . log(data . tostring())})})。listen(3000,function(){ console . log(' server start 3000 ');});Http客户端:
http模块提供HTTP.request和http.get两种方法来创建HTTP客户端,从而向HTTP服务器发起请求。Http。get是http.request的快捷方式,只支持GET模式下的请求。
http.request(选项,回调)方法启动一个http请求。选项是请求的参数,回调是请求的回调函数,在请求被响应后执行。它传递一个参数,该参数是http的一个实例。ClientResponse并处理返回的数据。
选项常用参数如下:
1)主机:请求网站的域名或IP地址。2)端口:请求网站的端口,默认为80。3)方法:请求方法,默认为GET。4)路径:相对于根的请求路径默认为“/”。应该包括请求参数。5)头:请求头的内容。
事实上,nodejs实现的爬虫可以使用http模块创建的客户端向我们想要抓取数据的地址发送请求,并获取响应数据进行分析。
得到
//http客户端const http=require(' http ');//发送请求的配置let config={ host: 'localhost ',port: 3000,path:'/',method: 'GET ',header RS : { a : 1 } }//创建客户端let client=http.request(config,function(res) { //接收服务端返回的数据让repData=res.on('data ',function(data){ repData=data。tostring()。控制台。log(repData)});res.on('end ',function(){//console。日志(缓冲区。concat(arr)).toString());});});//发送请求客户。end();结束请求,否则服务器将不会收到信息客户端发起超文本传送协议(超文本传输协议的缩写)请求,请求方法为去拿,服务端收到得到请求,匹配路径是首页,响应数据:好吧。
邮政
//http客户端var http=require(' http ');var查询字符串=require('查询字符串');var contents=查询字符串。stringify({ name : '艾利斯提,email :“[email protected]”,address:”成都',});var选项={ host : ' localhost ',port: 3000,path:'/users ',method: 'POST ',header RS : { ' Content-Type ' : ' application/x-www-form-URL编码',' Content-Length ' : contents。长度} };var req=http.request(options,function(RES){ RES . setencoding(' utf8 ');res.on('data ',function(data){ console。日志(数据);})})请求。写(内容);//结束请求,否则服务器将不会收到信息请求。end();//响应的数据为{ '用户1 ' : { '姓名' : ' mahesh ','密码' : 'password1 ','职业' : '教师,' id': 1 },' user2' : { 'name' : 'suresh ',' password' : 'password2 ',' profession ' : '图书管理员,' id ' : 2 } }客户端发起超文本传送协议(超文本传输协议的缩写)请求,请求方法为邮政,邮政传递数据,匹配路径是/用户,服务器响应请求并返回数据user.json里的内容。
放
//http客户端const http=require(' http ');//发送请求的配置let config={ host: 'localhost ',port: 3000,path:'/list ',method: 'put ',header RS : { a : 1 } }//创建客户端let client=http.request(config,function(res) { //接收服务端返回的数据让repData=res.on('data ',function(data){ repData=data。tostring()。控制台。log(repData)});res.on('end ',function(){//console。日志(缓冲区。concat(arr)).toString());});});//发送请求客户。end();客户端发起超文本传送协议(超文本传输协议的缩写)请求,请求方法为放,服务端收到放请求,匹配路径为/list,响应数据:放好
删除
//http删除请求客户端var http=require(' http ');var查询字符串=require('查询字符串');var contents=查询字符串。stringify({ name : '艾利斯提,email :“[email protected]”,address:”成都',});var选项={ host : ' localhost ',port: 3000,path:'/detail ',method: 'DELETE ',header RS : { ' Content-Type ' : ' application/x-www-form-URL编码',' Content-Length ' : contents。长度} };var req=http.request(options,function(RES){ RES . setencoding(' utf8 ');res.on('data ',function(data){ console。日志(数据);})})请求。写(内容);请求。end();服务端收到删除请求,匹配路径为/详细信息,响应数据:删除确定
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
版权声明:详细说明了net和http在Nodejs核心模块中的使用是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。