簡體   English   中英

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

[英]How to fix ETIMEDOUT of webpack devServer proxy (ignoring corporate proxy?)

在我的本地開發環境中,我使用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的任何想法?

有任何想法嗎?

由於我找不到有關如何代理devServer.proxy的任何內容,因此找到了一種解決方法:

我們需要使用另一個本地代理通過公司代理發送請求。 這可以通過運行httpServer(localhost:9009)並獲取請求的nodejs腳本來完成,我希望將其發送到https://myProject.atlassian.net/rest/api/2 然后,此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"
},

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM