Gzip not working

落爺英雄遲暮 提交于 2019-12-13 03:36:38

问题


I am trying to handle gzip.

My sources: zlib,compression,https(article by Rob W)

Server-Side:

    app.get('*', function (req, res, next) {
    if (req.headers['x-forwarded-proto'] != 'https') {
        res.setHeader('Content-Type', 'text/event-stream')
        res.setHeader('Cache-Control', 'no-cache')

        // send a ping approx every 2 seconds
        var timer = setInterval(function () {
            res.write('data: ping\n\n')

            // !!! this is the important part
            res.flush()
        }, 2000)

        res.on('close', function () {
            clearInterval(timer)
        })

        res.redirect('https://...herokuapp.com' + req.url)//req.connection.remoteAddress
    }
    else {
        next();
    }
})

Error:

events.js:85 throw er; // Unhandled 'error' event ^ Error: write after end at ServerResponse.OutgoingMessage.write (_http_outgoing.js:413:15) at ServerResponse.res.write (...\index.js:80:17) at null. (...\app.js:63:17) at wrapper [as _onTimeout] (timers.js:265:14) at Timer.listOnTimeout (timers.js:110:15)

Process finished with exit code 1

Client side request:

<link rel="stylesheet" type="text/css" href="../.../.../....min.css.gz">

回答1:


You can't res.write() after res.redirect(). The latter ends the response.

You might consider creating a dedicated route for your Server-Sent Events stream instead.



来源:https://stackoverflow.com/questions/32283346/gzip-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!