栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Node.js:分块传输编码

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Node.js:分块传输编码

如果要进行分块传输编码,则实际上需要设置该标头:

Transfer-Encoding: chunked

您可以从google返回的标头中看到该标头,该标头会对主页以及最可能的其他页面进行分块传输:

HTTP/1.1 200 OKDate: Sat, 04 Jun 2011 00:04:08 GMTExpires: -1Cache-Control: private, max-age=0Content-Type: text/html; charset=ISO-8859-1Set-cookie: PREF=ID=f9c65f4927515ce7:FF=0:TM=1307145848:LM=1307145848:S=fB58RFtpI5YeXdU9; expires=Mon, 03-Jun-2013 00:04:08 GMT; path=/; domain=.google.comSet-cookie: NID=47=UiPfl5ew2vCEte9JyBRkrFk4EhRQqy4dRuzG5Y-xeE---Q8AVvPDQq46GYbCy9VnOA8n7vxR8ETEAxKCh-b58r7elfURfiskmrOCgU706msiUx8L9qBpw-3OTPsY-6tl; expires=Sun, 04-Dec-2011 00:04:08 GMT; path=/; domain=.google.com; HttpOnlyServer: gwsX-XSS-Protection: 1; mode=blockTransfer-Encoding: chunked

编辑 Yikes,读起来太复杂了:

var app = function(req,res){ var head={'Content-Type':'text/html'} switch(req.url.slice(-3)){  case '.js':head={'Content-Type':'text/javascript'};break;  case 'css':head={'Content-Type':'text/css'};break;  case 'png':head={'Content-Type':'image/png'};break;  case 'ico':head={'Content-Type':'image/x-icon'};break;  case 'ogg':head={'Content-Type':'audio/ogg'};break;  case 'ebm':head={'Content-Type':'video/webm'};break; } res.writeHead(200,head) var file_stream = fs.createReadStream('.'+req.url); file_stream.on("error", function(exception) {   console.error("Error reading file: ", exception); }); file_stream.on("data", function(data) {   res.write(data); }); file_stream.on("close", function() {   res.end(); });}

到了那里,一个不错的流缓冲供您编写。 这是我写的一篇博客文章,介绍了几种读取文件的方法。
我建议您仔细研究一下,以便您可以了解如何在节点的异步环境中更好地处理文件。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/448627.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号