NodeJs——(7)request的结构

简介: (24)request的结构 这里指的是发送的结构: 先上代码: var http = require("http");var util = require("util");var server = new http.Server();server.on("request", function (req, res) { res.writeHead(200, {'Co

(24)request的结构

这里指的是发送的结构:

先上代码:

var http = require("http");
var util = require("util");
var server = new http.Server();
server.on("request", function (req, res) {
    res.writeHead(200, {'Content-type': 'text/html;charset = utf8'});
    res.write("<h1>这里是一个头</h1>");
    res.write("是否完成:" + req.complete);
    res.write("</br>Http版本协议:" + req.httpVersion);
    res.write("</br>请求方法:" + req.method);
    res.write("</br>原始请求路径:" + req.url);
    res.write("</br>请求头:" + util.inspect(req.headers)); //将其转为字符串
    res.write("</br>请求尾:" + util.inspect(req.trailers));
    res.write("</br>当前连接套接字:" + req.connetion);
    res.write("</br>上面的别名:" + req.socket);
    res.write("</br>client属性的别名:" + req.client);
    res.end();
})
server.listen(80);
console.log("The server begin");

结果为:


这里是一个头

是否完成:false
Http版本协议:1.1
请求方法:GET
原始请求路径:/
请求头:{ host: '127.0.0.1', connection:'keep-alive', 'cache-control': 'max-age=0', 'upgrade-insecure-requests': '1','user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36', accept:'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','accept-encoding': 'gzip, deflate, sdch', 'accept-language': 'zh-CN,zh;q=0.8' }
请求尾:{}
当前连接套接字:undefined
上面的别名:[object Object]
client属性的别名:[objectObject]



另外,请求头是一个对象,因此可以列出他的属性,使用for in遍历之,如代码:

var http = require("http");
var util = require("util");
var server = new http.Server();
server.on("request", function (req, res) {
    res.writeHead(200, {'Content-type': 'text/html;charset = utf8'});
    for (var i in req.headers) {
        res.write("req.headers[" + i + "]: " + req.headers[i] + "</br>")
    }
    res.end();
})
server.listen(80);
console.log("The server begin");

结果:

req.headers[host]: 127.0.0.1
req.headers[connection]: keep-alive
req.headers[cache-control]: max-age=0
req.headers[upgrade-insecure-requests]: 1
req.headers[user-agent]: Mozilla/5.0 (Windows NT 6.1; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36
req.headers[accept]: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
req.headers[accept-encoding]: gzip, deflate, sdch
req.headers[accept-language]: zh-CN,zh;q=0.8

 


另外,很奇怪的是,我试着使用req.headers[cache-control]

或者是req.headers.cache-control,都无法出来正常的结果,不明白。

 

关于cache-control的取值

http://www.phpernote.com/seo/465.html


目录
相关文章
|
前端开发
前端报错node-pre-gyp WARN Using request for node-pre-gyp https download解决方法
前端报错node-pre-gyp WARN Using request for node-pre-gyp https download解决方法
933 0
|
JSON JavaScript 算法
Node第三方包 【Request】
Node第三方包 【Request】
152 0
|
Web App开发 JavaScript Windows
NodeJs——(7)request的结构
(24)request的结构 这里指的是发送的结构: 先上代码: var http = require("http"); var util = require("util"); var server = new http.Server(); server.on("request", function (req, res) { res.writeHead(200, {'Co
1377 0
|
JavaScript
nodejs Error: request entity too large解决方案
错误如图: 解决方案: app.js添加 var bodyParser = require('body-parser'); app.use(bodyParser.json({limit: '50mb'})); app.
1720 0