zoukankan      html  css  js  c++  java
  • Nodejs 请求转发代理

    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();
    
    }
  • 相关阅读:
    android 学习
    android 学习
    阅读笔记《人月神话》1
    android 学习
    android 学习
    android 学习
    android 学习
    android 学习(家庭记账本的开发 6)
    每日日报
    每日日报
  • 原文地址:https://www.cnblogs.com/wanglao/p/5948016.html
Copyright © 2011-2022 走看看