我正在尝试使用节点的https.request()函数将API请求代理到服务器。当我通过Postman向相同的端点运行相同的请求时,我会按预期返回数据。当我通过节点代理运行请求时,我得到了http 500错误代码。我发送请求的方式有什么明显错误:
#!/usr/bin/env node
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var https = require('https'),
connect = require('connect'),
staticDir = 'target/ui';
var app = connect();
app.use(connect.logger('dev'))
.use(connect.bodyParser())
.use(function(req, res, next){
if( /^\/api\//.test(req.url) ){
var options = {
hostname: 'host.name',
port: 9090,
path: req.url,
method: req.method,
headers: {
authorization: 'Basic XYZ123',
'content-type': 'application/json',
accept: 'application/json, text/javascript, */*; q=0.01',
}
};
var proxyRequest = https.request(options).on('response', function(res){
var data = "";
res.on('data', function(chunk){
data += chunk;
})
res.on('end', function(){
sendData(data);
});
res.on('error', function(e){
console.log('ERROR: ', e);
sendError(e, code);
})
})
proxyRequest.end();
function sendData(data){
console.log('SENDING RESPONSE DATA', data)
res.write(data);
res.end();
}
function sendError(msg, code){
res.end(msg, code)
}
}
else {
next();
}
})
.use(connect.static(staticDir));;
app.listen(9090);