1
var express = require('express')
var app = module.exports = express()
function getImages(callback) {
callback()
}
app
.set('views', __dirname + '/views')
.set('view engine', 'jade')
.get('/stream/images', function(req, res) {
res.render('layout', function(err, body) {
res.type('html')
res.write(body.replace('</body></html>', ''))
getImages(function(err) {
res.render('streams/images', function(err, body) {
var html = '<div class="layout-stream">' + body + '</div>'
res.write(
"<script>$('.pagelet-layout-stream').replaceWith(" +
JSON.stringify(html) +
"); emitter.emit('check:stream');</script>"
)
res.write("<script>emitter.emit('load');</script>")
res.write('</body></html>')
res.end()
})
})
})
})
.listen(3013)
이것은 의도 한대로 작동합니다. 그러나 .use(express.compress())
(정확하게는 .get
이전) 페이지가 더 이상 스트림되지 않습니다. 즉, express.compress()
은 응답이 완료 될 때까지 기다린 후 전체 응답을 gzip 한 다음 전송합니다. 각 res.write
에 gzipped 응답 (또는 특히 모든 유출 물)을 보내고 싶습니다. 응답을 올바르게 압축하는 방법은 무엇입니까?res.write를 사용하여 스트리밍 컨텐츠를 압축/압축/압축 해제
var stream = zlib.createGzip()
stream.pipe(res)
stream.write(/* stuff */)
stream.end()
이 정확히 express.compress()
처럼 작동 : -
편집 한 나는이 시도했다. 나는 브라우저가 모든 것을 가질 때까지 브라우저가 단지 시체를 파싱하는지 여부를 확신하지 못한다. 그렇지 않으면 응답이 완료 될 때까지 시체를 보내지 않는다.