如何修复Webpack devServer代理的ETIMEDOUT(忽略公司代理?)

时间:2019-06-25 15:04:20

标签: webpack proxy webpack-dev-server

在我的本地开发环境中,我使用webpack devServer.proxy将API请求代理到https://.atlassian.net/rest/api/2,以抑制CORS问题。

但是请求超时

[HPM] Error occurred while trying to proxy request /project from localhost:9008 to https://<myProject>.atlassian.net/rest/api/2 (ETIMEDOUT) (https://nodejs.org/api/errors.html#errors_common_system_errors)

我想问题是,我位于代理后面的公司网络中,要访问Internet,我需要使用代理。代理设置包含在/ etc / profile(MacOS)中。

当我在相同的外壳中使用curl进行类似的请求时

curl --request GET --url 'https://<myProject>.atlassian.net/rest/api/2/project' --header 'Authorization: Basic <someBase64EncodedString>' --header 'Accept: application/json'

我得到一个有效的答案(我有权访问的所有项目的列表)。使用webpack-dev-server运行相同的请求时,超时(请参见上文)。

这是我的webpack devServer.proxy配置:

'/api/*': {
            target: 'https://<myProject>.atlassian.net/rest/api/2',
            headers: {
                Authorization: 'Basic <someBase64EncodedString>',
                Accept: "application/json"
            },
            secure: false,
            changeOrigin: true,
            pathRewrite: {
                '^/api': ''
            },
            logLevel: 'debug'
        }

任何想法为何webpack都会忽略/ etc / profile中的https_proxy?

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

由于我没有找到有关如何代理devServer.proxy的任何内容,因此我找到了一种解决方法:

我们需要使用另一个本地代理通过公司代理发送请求。这可以通过运行httpServer(localhost:9009)并获取请求(我想发送至https://myProject.atlassian.net/rest/api/2)的nodejs脚本来完成。然后,此httpServer会将我的请求发送到公司代理。 Webpack.devServer.proxy配置现在看起来像这样:

'/api/*': {
        target: 'http://localhost:9009',
        headers: {
            Authorization: 'Basic <someBase64EncodedString>',
            Accept: "application/json"
        },
        secure: false,
        changeOrigin: true,
        pathRewrite: {
            '^/api': ''
        },
        logLevel: 'debug'
    }

httpServer的“ postproxy.js”脚本如下所示:

var http = require('http');

var request = require('./node_modules/request');
var fs = require('fs');
var proxy = "<corporate proxy url>";
var api = "https://<myProject>.atlassian.net/rest/api/2/";
var hopper = "https://<myProject>.atlassian.net/rest/greenhopper/1.0";

http.createServer(function (reqorg, resorg) {

if (reqorg.method == 'POST'){
    var bodyorg = '';
    reqorg.on('data', function (data) {
        bodyorg += data;
    });
    reqorg.on('end', function () {

        var head = {
            "content-type" : "application/json",
            "Authorization": reqorg.headers.authorization
        };

        if(reqorg.url.includes("attachment")){
            // Adjusting Head for Attachment Transfer
            head["X-Atlassian-Token"] = "no-check";
            head["content-type"] = "multipart/form-data";

            var content = JSON.parse(bodyorg);
            var buffer = Buffer.from(content.file,'base64');

            var options = {
                headers: head,
                uri: api+reqorg.url,
                formData: {
                    file: {
                        value: buffer,
                        options: {
                            filename: content.filename
                        }
                    }
                },
                method: 'POST'
            }

            request(options, function(err, response, body){
                resorg.writeHead(200, {'Content-Type': 'application/json'});
                resorg.end(body);
            });

        } else {

            request.post({
                headers: head,
                url: api+reqorg.url,
                proxy: proxy,
                body: bodyorg
            }, function(error, response, body){
                resorg.writeHead(200, {'Content-Type': 'application/json'});
                resorg.end(body);
            });

        }
    });
} else if (reqorg.method == 'GET') {
    request.get({
        headers: {
            'content-type' : 'application/json',
            'Authorization': reqorg.headers.authorization
        },
        url: api + reqorg.url
    }, function (error, response, body) {
        resorg.writeHead(200, {'Content-Type': 'application/json'});
        resorg.end(body);
    })
} else if (reqorg.method == 'DELETE') {
    request.delete({
        headers: {
            'content-type' : 'application/json',
            'Authorization': reqorg.headers.authorization
        },
        url: api + reqorg.url
    }, function (error, response, body) {
        resorg.writeHead(200, {'Content-Type': 'application/json'});
        resorg.end(body);
    });
} else if (reqorg.method == 'PUT') {
    var bodyorg = '';
    reqorg.on('data', function (data) {
        bodyorg += data;
    });
    reqorg.on('end', function () {
        request.put({
            headers: {
                'content-type' : 'application/json',
                'Authorization': reqorg.headers.authorization
            },
            url: hopper+reqorg.url,
            proxy: proxy,
            body: bodyorg
        }, function(error, response, body){
            resorg.writeHead(200, {'Content-Type': 'application/json'});
            resorg.end(body);
        });
    });
}

}).listen(9009);

不要忘记在package.json中启动它:

"scripts": {
    "start": "webpack-dev-server --mode development | npm run proxy",
    "proxy": "node postproxy.js"
},