What is the correct way to stream an index page to a client?

In most examples you find on the web, an `index.html` file is served like the following: function serveIndexPage(response) { fs.readFile('__dirname + /public/index.html', function (err, data) { response.end(data); }); }; This seems like a bad idea, as the whole file is read into memory and then send to the client. Is there some better way to do this? I know that libaries like Connect and Express provide such a functionality, but for my project, I'd like to use plain node.js. **EDIT** Also, you sometimes see `readFileSync` used, which is even worse IMHO.
You could use fs.createReadStream(file).pipe(response) which might improve performance, but if you're worried about this kind of performance, I suggest you serve static files using a static file server or a CDN.

以上就是What is the correct way to stream an index page to a client?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » Node.js答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏