2

Trying to fetch JSON data from third party url and bring it to my backend route. First, I am getting query string entered into my application's url and store them to variables and use it in third party url. Second and third query strings are not entering into the url although the query string from application url has been stored properly. Wondering if there is a proper way of doing such thing.

Also, when the JSON data is fetched, is there a way to make the format it cleaner, rather than single line of an object of 47 arrays that is overflowed into 4 lines?

app.get("/api/posts", (req, res) => {
  const first = req.query.first;
  const second = req.query.second;
  const third = req.query.third;
  axios.get(`url.com?first=${first}&second=${second}&third=${third}`)
    .then(response=> res.json(response.data)
    .catch(err => res.send(err))
})
Jinman Kim
  • 31
  • 3
  • The snippet you've posted is a common way to do that (when you will fix syntax errors). But if you do not always care about handling response data or request error, you can just pipe response stream into request stream res.pipe(axios.get('YOUR_URL")) – Gena Moroz Aug 06 '19 at 15:22
  • Thank you for the suggestions. I might have noticed the syntax errors if I was writing on my visual studio. – Jinman Kim Aug 06 '19 at 15:43
  • What is the advantage of using the pipe?? and also I still need a suggestion on how to get the second and third query parameters working – Jinman Kim Aug 06 '19 at 15:43
  • You are still using the wrong quotes and there are syntax errors. *No* parameters will work with that code. Please provide a [mcve]. – str Aug 06 '19 at 15:45
  • That should be better. Couldn't find the backtick symbol on my work MacBook with french keyboard. Copied and pasted from Google now. – Jinman Kim Aug 06 '19 at 15:52
  • remember that streaming data into a response is also a great way to expose data that should never be exposed, because as devs we all make mistakes and sometimes the pipe will be for the wrong thing. So usually, even though it's a bit more processing before writing to the response, _verifying the data_ before sending it to your user is 100% recommended. – Mike 'Pomax' Kamermans Aug 06 '19 at 15:54

0 Answers0