码迷,mamicode.com
首页 > Web开发 > 详细

Nodejs 请求转发代理

时间:2016-10-11 11:24:33      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

var sendPromise = function (res, callback) {

    var options = {
        hostname: settings.Ip,
        port: settings.Port,
        path: ‘‘,
        method: ‘GET‘,
        headers: res.req.headers
    };
    options.path = res.req.originalUrl;
    options.method = res.req.method;
    var body = res.req.body;


    var request = http.request(options, function (response) {
        var result = ‘‘;
        response.setEncoding(‘utf8‘);
        response.on(‘data‘, function (chunk) {
            result += chunk;
        });
        response.on(‘end‘, function () {

            callback(null, result);
        });
    });


    request.on(‘error‘, function (e) {
        callback(‘path:‘ + options.path + ‘;‘ + e, null);
    });

    if (body && options.method == ‘POST‘) {
        var postBody = {};
        for (var k of Object.keys(body)) {
            postBody[k] = body[k];
        }
        request.write(postBody);
    }
    request.end();

}

 

Nodejs 请求转发代理

标签:

原文地址:http://www.cnblogs.com/wanglao/p/5948016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!