1
votes

I'm trying console log the json for an API after sending my request through a proxy, however when the code runs the console logs nothing. Here is my code.

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); });

});

Is there a fix, or a better way to go about doing this?

BELOW IS A LOG WITH THE SUGGESTED CHANGES

(using 'request-promise' in both requests and logging the error, and the ip address.)

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
}
1
Which console.log statement do you expect to do anything? Also, running this code will do nothing but to open a http server on port 3000 (which should log listening at 3000), are you actually sending a request to the /api route? Please show us how you are sending that request, and notice that your server never responds to that request. - Bergi
You're making 3 (and counting the external proxies, 5) requests here, which is way too many! Which of them does not work? - Bergi
Why are you mixing request and request-promise in the same code? - Bergi
I am actually sending a request to the /api route, and it definitely works as whatever is sent gets logged to the console. I may just be confused and trying the wrong method to send my API request through a proxy, if thats the case do you know another way I could go about doing it? - LostInTheCSSauce
It seems to be the final request that isn't working. - LostInTheCSSauce

1 Answers

0
votes

You have an if statement. If the statuscode is not 200, but is somehow still accepted as 'not an error', the error catch does not work, so add an else to that if and in it log an error message, or just delete the if statement to get the response no matter what it may be.