[英]Returning API JSON through a proxy in NodeJS
在通过代理发送我的请求后,我正在尝试控制台记录 json 的 API,但是当代码运行时,控制台不会记录任何内容。 这是我的代码。
const express = require('express');
const app = express();
const fetch = require('node-fetch')
app.listen(3000, () => console.log('listening at 3000'));
app.use(express.static('public'));
app.use(express.json())
app.post('/api', (request, response) => {
let steamIdValue = request.body.steamId
let steamInventoryApiLink = 'https://steamcommunity.com/profiles/' + steamIdValue + '/inventory/json/730/2'
require('request-promise')({
url: 'xxxxx',
proxy: 'xxxxx'
}).then(function(data){
const request = require('request');
request({
'url': steamInventoryApiLink,
'method': "GET",
'proxy':'http://' + data + ':80'
},function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
})
}, function(err){ console.error(err); });
});
go 是否有解决方案或更好的方法来执行此操作?
以下是建议更改的日志
(在请求中使用“request-promise”并记录错误,以及 ip 地址。)
Aleeshas-MacBook-Pro:LoopTradesRound2 aleeshamoseley$ node index.js
listening at 3000
193.8.1.88
RequestError: Error: tunneling socket could not be established, cause=connect ECONNREFUSED 193.8.1.88:80
at new RequestError (/Users/aleeshamoseley/Desktop/LoopTradesRound2/node_modules/request-promise-core/lib/errors.js:14:15)
at Request.plumbing.callback (/Users/aleeshamoseley/Desktop/LoopTradesRound2/node_modules/request-promise-core/lib/plumbing.js:87:29)
at Request.RP$callback [as _callback] (/Users/aleeshamoseley/Desktop/LoopTradesRound2/node_modules/request-promise-core/lib/plumbing.js:46:31)
at self.callback (/Users/aleeshamoseley/Desktop/LoopTradesRound2/node_modules/request/request.js:185:22)
at Request.emit (node:events:527:28)
at Request.onRequestError (/Users/aleeshamoseley/Desktop/LoopTradesRound2/node_modules/request/request.js:877:8)
at ClientRequest.emit (node:events:527:28)
at ClientRequest.onError (/Users/aleeshamoseley/Desktop/LoopTradesRound2/node_modules/tunnel-agent/index.js:179:21)
at Object.onceWrapper (node:events:642:26)
... 5 lines matching cause stack trace ...
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
cause: Error: tunneling socket could not be established, cause=connect ECONNREFUSED 193.8.1.88:80
at ClientRequest.onError (/Users/aleeshamoseley/Desktop/LoopTradesRound2/node_modules/tunnel-agent/index.js:177:17)
at Object.onceWrapper (node:events:642:26)
at ClientRequest.emit (node:events:527:28)
at Socket.socketErrorListener (node:_http_client:454:9)
at Socket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ECONNRESET'
},
error: Error: tunneling socket could not be established, cause=connect ECONNREFUSED 193.8.1.88:80
at ClientRequest.onError (/Users/aleeshamoseley/Desktop/LoopTradesRound2/node_modules/tunnel-agent/index.js:177:17)
at Object.onceWrapper (node:events:642:26)
at ClientRequest.emit (node:events:527:28)
at Socket.socketErrorListener (node:_http_client:454:9)
at Socket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ECONNRESET'
},
options: {
url: 'https://steamcommunity.com/profiles/76561198034202275/inventory/json/730/2',
method: 'GET',
proxy: 'http://193.8.1.88:80',
callback: [Function: RP$callback],
transform: undefined,
simple: true,
resolveWithFullResponse: false,
transform2xxOnly: false
},
response: undefined
}
你有一个 if 语句。 如果状态码不是 200,但仍以某种方式被接受为“非错误”,则错误捕获不起作用,因此在 if 中添加 else 并在其中记录错误消息,或者删除 if 语句以获取不管是什么反应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.