Поскольку я не нашел ничего о том, как прокси-сервер devServer.proxy, я нашел обходной путь:
Нам нужно использовать другой локальный прокси для отправки запроса через корпоративный прокси. Это можно сделать с помощью сценария nodejs, который запускает httpServer (localhost: 9009) и получает запрос, который я хотел отправить на 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"
},