前言
当request对象关闭,完成或者报错的时候会执行回调函数。
安装
npm install on-finished
依赖
var onFinished = require('on-finished')
用法
onFinished(res,listener)或者onFinished(req,listener)
例子
var destroy = require('destroy')
var http = require('http')
var onFinished = require('on-finished')
http.createServer(function onRequest(req,res) {
var stream = fs.createReadStream('package.json')
stream.pipe(res)
onFinished(res,function (err) {
destroy(stream)
})
})