Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing response from API as response from my node server throws

In certain cases, when a specific route is hit on my node/express server, I would like to make a request to an API and return that response directly to the client. I followed this stack overflow post: Send response from server side axios request to React/Redux app

To create this:

router.use('/', async (req, res) => {
  const apiUrl = `https://assets.scratch.mit.edu${req.url}`;
  console.log(apiUrl);
  const scratchResponse = await axios.get(apiUrl);
  try {
    res.send(scratchResponse);
    console.log('worked!', apiUrl);
  } catch (error) {
    console.log('error in assets get request', apiUrl, error);
    res.sendStatus(500);
  }
});

This is working for some of the requests, but breaks for others with the error:

TypeError: Converting circular structure to JSON

Per the suggestions from this stack overflow post: JSON.stringify, avoid TypeError: Converting circular structure to JSON

I have tried using the npm flatted library by adding this import:

const { stringify } = require('flatted/cjs');

and stringifying before my response:

res.send(stringify(scratchResponse));

But it seems like some information is being removed that is critical to the client (client breaks and says it is missing properties).

Is it possible to directly send the response I'm receiving from the API?

like image 619
Luke Schlangen Avatar asked Mar 03 '19 19:03

Luke Schlangen


Video Answer


1 Answers

Well, the easiest way would probably be to pipe the response down to the client:

  router.get('/', (req, res) => {
    axios({
      method: 'get',
      url: apiUrl,
      responseType: 'stream'
    })
      .then(function(response) {
        response.data.pipe(res)
    });
  });

If there's an issue with the format from the third party, you're still out of luck, but this will at least take your server-side processing out of the mix as the problem, and also performance should be a little better since you're just piping the response down rather than getting the full response on the server, serializing it to JSON only to re-send it to the response stream.

like image 173
Paul Avatar answered Oct 12 '22 05:10

Paul