Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http 模块 -- http.IncomingMessage类 #14

Open
mt51 opened this issue Oct 25, 2018 · 0 comments
Open

http 模块 -- http.IncomingMessage类 #14

mt51 opened this issue Oct 25, 2018 · 0 comments

Comments

@mt51
Copy link
Owner

mt51 commented Oct 25, 2018

http 模块 -- http.IncomingMessage类

http.IncomingMessage对象由http.server或者是http.clientRequest创建,并作为第一个参数分别传递给'request'事件和'response'事件。可以用来访问响应状态、消息头以及数据。

const http = require('http')
const options = {}
const client = http.request(options)
client.on('response', res => {})   //  res 是http.IncomingMessage的一个实例

const server = http.createServer()

server.on('request', (req, res) => {})  //  req 是http.IncomingMessage的一个实例

'aborted'事件

当请求已被终止且网络已关闭时触发

'close'事件

当底层连接被关闭时触发。 同 'end' 事件一样,该事件每个响应只触发一次。

message.aborted

判断请求是否已被终止

message.destroy([error])

该方法会调用socket上的destroy()方法。如果提供了error参数,则会触发'error'事件,并将error参数传入事件监听器。

message.headers

获取请求头或者响应头对象

message.httpVersion

获取HTTP的版本

message.method

获取请求的方法,只能在http.Server中使用

message.rawHeaders

接收到的原始的请求头或响应头列表。键和值在同一个列表中。 偶数位的是键,奇数位的是对应的值。

头信息的名称不会被转换为小写,重复的也不会被合并。

message.statusCode

仅在http.ClientRequest中有效,返回HTTP状态码

message.url

仅在http.ClientRequest中有效,返回请求中的URL,包括查询参数。

const http = require('http')

const server = http.createServer()

server.on('request', (req, res) => {
    res.end()
    req.on('end', () => {
        console.log('end')
    })
    console.log('http版本')
    console.log(req.httpVersion)
    console.log('请求方法')
    console.log(req.httpVersion)
    console.log('请求头')
    console.log(req.headers)
})

server.listen(3000, () => {
  console.log('server is running at localhost:3000')
})

server.on('error', error => {
  console.log(error)
})

参考

node中文文档 http模块

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant