var options = this.options;
var len = stat.size;
var res = this.res;
var req = this.req;
var ranges = req.headers.range; //截取部分文件之用
var offset = options.start || 0;
//赋值header
this.setHeader(stat);
//赋值 content-type
this.type(path);
//条件 GET 支持, isFresh() 见 [fresh](https://github.com/visionmedia/node-fresh/blob/master/index.js)
if (this.isConditionGET()
&& this.isCachable()
&& this.isFresh()) {
return this.notModified();
}
//
len = Math.max(0, len - offset);
if (options.end != undefined) {
var bytes = options.end - offset + 1;
if (len > bytes) len = bytes;
}
// Range 支持
if (ranges) {
...
}
// content-length
res.setHeader('Content-Length', len);
// HEAD support
if ('HEAD' == req.method) return res.end();
this.stream(path, options); //见下
方法 stream
参数
path : 路径
options
1234567891011121314151617181920212223242526272829
var self = this;
var res = this.res;
var req = this.req;
//pipe 把流数据加入 `res`管道
var stream = fs.createReadStream(path, options);
this.emit('stream', stream);
stream.pipe(res);
//socket 关闭,
req.on('close', stream.destroy.bind(stream));
//error处理
steam.on('error', function(err){
//不回复
if (res._header) {
console.error(err.stack);
req.destroy();
return;
}
erro.status = 500;
self.emit('error', err);
});
//end
stream.on('end', function(){
self.emit('end');
});