HTTP代理:拒绝半处理请求

时间:2015-12-14 15:37:52

标签: javascript node.js http caching http-proxy

我有基于Node.js的HTTP代理,它以流方式转换响应主体。例如,我的代理就像nginx一样,它压缩(使用libz for gzipping)响应体。 Overall scheme:

但我有一个悬而未决的问题:如果在数据交换期间上游连接发生错误怎么办?如何通知客户端有关错误,何时已发送响应且正在传输正文。客户端错误确定的复杂性基于Content-Length因源和转换数据不匹配而无响应的事实。

为了澄清一些细节,我添加了一段简单的代码:



var express     = require("express");
var http        = require("http");
var url         = require('url');


var webApp = express();

var httpServer = webApp.listen(8080, function () {
    console.log("server started on ", 8080, " port");
});

webApp.get("*", function(req, res) {

    var targetRequest = http.get(req.originalUrl, function (upstreamResponse) {
       
        if (upstreamResponse.statusCode != 200) {
            res.status(500).send("Internal Server Error");
            return;
        }

        upstreamResponse.on("data", function (chunk) {
            /// transform chunk and send it to the client
            res.write(chunk);
        });

        upstreamResponse.on("end", function () {
            res.end();
        });

        /// upstream connection error occured
        upstreamResponse.on("error", function (error) {
            /// How to properly notify client
            /// about error ????
            /// ????
        });

    });

});




1 个答案:

答案 0 :(得分:0)

实际上,通知客户有关某些问题的唯一方法就是放弃下游连接。