score:0

you want to take the date state from your client and send it to your api endpoint through a post request. your api endpoint will then take the date from the req.body and insert that into it's own request to the nasa api. your api will await the nasa api's response and send that back to the client once it is received.

client

await axios({
  method: "post",
  url: "yourapi/customdate",
  data: {
    date: this.state.date 
  },
  withcredentials: false
}).then((response) => {
  //response from your api that includes the nasa api image
})

server

router.post("/customdate", async (req, res) => {
  const customdate = req.body.date
  await axios({
    method: "get",
    url: `https://api.nasa.gov/planetary/apod?api_key=${process.env.react_app_api_key_nasa}&date=${customdate}`,
    withcredentials: false
  }).then((response) => {
    res.send(response.image)
  })
})

score:1

you should have one more axios call from the react to your nodejs as react(front-end) and nodejs(back-end) will be two different things.

it is either you call directly from react to nasa api (which will exposed your api key in the front-end) or react axios call to your nodejs and then nodejs axios call (api key) to nasa api.


Related Query

More Query from same tag