Node.js学习教程之HTTP/2服务器推送【译】

2020-06-17 07:05:47易采站长站整理

})
}

function onRequest (req, res) {
// Push files with index.html
if (reqPath === '/index.html') {
push(res.stream, 'bundle1.js')
push(res.stream, 'bundle2.js')
}

// Serve file
res.stream.respondWithFD(file.fileDescriptor, file.headers)
}

这样bundle1.js和bundle2.js资源即使在它要求它们之前也会被发送到浏览器。

你可以查看完整的案例:https://github.com/RisingStack/http2-push-example

HTTP/2 & Node

HTTP/2可以帮助我们在很多方面优化我们的客户端与服务器之间的通信。

通过服务器推送,我们可以将资源发送到浏览器,减少用户的初始加载时间。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对软件开发网的支持。

原文:https://blog.risingstack.com/node-js-http-2-push/

译者:Jin

作者:Péter Márton