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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With