request(http.IncomingMessage)和response(http.ServerResponse)对象介绍

request:服务器解析用户提交的http请求报文,将结果解析到request对象中,凡是要获取和用户请求的数据都可以通过request对象获取

request对象常用类型,继承自stream.Readable
request.headers请求报文头
request.rawHeaders原生请求报文头
request.httpVersion请求版本号
request.method请求方法
request.url请求路径

response:在服务器端用来向用户做出响应的对象,凡是需要向用户(客户端)响应操作,都需要通过response对象来进行

response.writeHead(statusCode[, statusMessage][, headers])

response.setHeader

response.statusCode

responsestatusMessage

response.write

1.request

var http=require('http');
http.createServer(function(req,res){
//1.获取所有请求报文头
//req.headers返回的是一个对象,里面返回了所有的请求报文头
console.log(req.headers); res.end('over'); }).listen(9090,function(){
console.log('http://localhost:9090');
})

var http=require('http');
http.createServer(function(req,res){

//request.rawHeaders返回的是一个数组,数组里保存的都是请求报文头的字符串
console.log(req.rawHeaders);

        res.end('over');

    }).listen(9090,function(){
console.log('http://localhost:9090');
})

var http=require('http');
http.createServer(function(req,res){ //2.httpVersion
//获取请求的客户端所使用的http版本
console.log(req.httpVersion); res.end('over'); }).listen(9090,function(){
console.log('http://localhost:9090');
})

var http=require('http');
http.createServer(function(req,res){
//3.method
//获取客户端请求使用的方法(POST,GET......)
console.log(req.method); //4.url
//获取请求的路径(不包含主机名,端口号,协议)
console.log(req.url); res.end('over'); }).listen(9090,function(){
console.log('http://localhost:9090');
})

2.response

var http=require('http');
http.createServer(function(req,res){ //1.response.write(chunk[, encoding][, callback])
//chunk可以是一个字符串或者一个buffer,第二个参数指定如何将它编写出一个字节流,默认utf-8,当数据块被刷新的时候,callback会调用
res.write('hello world! 你好世界');
res.write('hello world! 你好世界'); //每个请求都必须调用res.end
//结束请求,该方法通知服务器,所有请求头和响应主体都以发送,服务器将视为已完成,可以考虑本次响应结束。
//res.end()要响应数据的话,数据必须是String或者Buffer类型
res.end('over'); }).listen(9090,function(){
console.log('http://localhost:9090');
})

但这样会出现乱码,所以需要加请求报文头

//2.通过res.setHeader来设置响应报文头,不过如果我们不设置响应报文头,系统也会默认有响应报文头,并且默认已经发送给了浏览器
res.setHeader('Content-Type','text/plain;charset=utf-8');

//3.设置http响应状态码
//res.statusCode设置http响应状态码
//res.statusMessage设置http响应状态码对应的消息
res.statusCode=404;
res.statusMessage='NOT FOUND';

这个是响应状态码,我们需要不同的响应状态码来反馈给用户信息,哪怕已经找到页面

//4. res.writeHead()
//直接向客户端响应(写入)http响应报文头
//建议在res.write()和res.end()之前调用
//如果res.statusCode,res.statusMessage,res.setHeader和res.writeHead一起设置,但是内容不一样,服务器会响应res.writeHead
res.writeHead(404,'not found',{
'Content-Type':'text/plain;charset=utf-8'
})

 

最新文章

  1. bootstrap表格分页
  2. 关于分工的思考 (Thoughts on Division of Labor)
  3. 用css进行布局
  4. Python 的三目运算
  5. windows 80端口被占用
  6. jsp页面指令
  7. 构建通过 Database.com 提供技术支持的 PhoneGap 应用程序
  8. sqlserver oracle 时间加减
  9. TortoiseSVN文件夹及文件图标不显示解决方法(转发)
  10. 【mongoDB高级篇①】聚集运算之group,aggregate
  11. Python pycurl
  12. 《A First Course in Probability》-chape4-离散型随机变量-几种典型分布列
  13. JSON取代XML?--JSON入门指南
  14. TD数量不确定时如何让其宽度平均分布
  15. 经常使用Javascript CDN 对照
  16. ios framework 开发实战 之 参考
  17. (cljs/run-at (JSVM. :all) "一起实现柯里化")
  18. Django之 HelloWorld
  19. Linux下Oracle表空间及用户创建
  20. AWT是Java最早出现的图形界面,但很快就被Swing所取代。

热门文章

  1. 使用canvas保存网页为pdf文件支持跨域
  2. Project Euler 30 Digit fifth powers
  3. [tyvj-1391]走廊泼水节 最小生成树
  4. 配置sudo命令行为审计
  5. C#使用 ComboBox 控件
  6. 【ACM-ICPC 2018 南京赛区网络预赛 E】AC Challenge
  7. JavaScript(DOM编程三)
  8. (7)JPA - Hibernate【从零开始学Spring Boot】
  9. 转-----------------------js window.open() 操作
  10. Arduino基本函数介绍