首页 Node.js核心模块手册

参数说明


on(event, callback)
event          事件名称
callback(      回调事件
    ?                根据事件规定,参考事件列表
)

事件列表


事件 示例 描述
response
ClientRequest.on("response", function(res){});

接收服务器返回响应流,request()方法的回调函数

res:参考:response

error
ClientRequest.on("error", function(err){});

请求数据发生错误

err.code 获取错误码:

socket
ClientRequest.on("socket", function(socket){
  socket.setTimeout(1000); //设置超时时间
  socket.on("timeout", fucntion(){
    ClientRequest.abort();  //终止连接
  })
});

socket:当连接服务器时,触发端口对象

注:例子中设置1秒内未收到服务器响应,则终止连接

注:终止后会触发error事件,错误码ECONNRESET

end ClientRequest.on("end", function(){}); ClientRequest.end()的回调函数