How to wait for a post request from another server before responding to a get request

49 Views Asked by At

I have a server to wrap an API and do extra stuff the user needs from my frontend.

Whenever my server receives a request, it does some processing it needs to and sends a POST request to a different server (out of my control). Now, this request takes a long time, so the request accepts a callback_url to which they promise to send a POST request to once the processing is complete. So, my code looks something like this:

app.get(`/my_url`, async (c) => {
  // do some processing

  const formData = new FormData()
  // append other stuff that I need
  formData.append(`callback_url`, `https://my-url.com/callback_here`)

  const response = await fetch(`external-url.com`, {
    method: `POST`,
    body: formData,
  }).then(response => response.json())

  //I immediately get a response that my stuff has successfully begun processing
    
})

app.post(`/callback_here`, async (c) =>{
  //here I`ll get a post request once the processing is done
}) 

So I need to write the code that app.get() is going to keep listening for the post request to come in to /callback_here and once that is done, process the rest and send the response back to the user.

How would I go about this? I assume there might be smarter ways to implement this, with which I have no idea how to begin going about.

0

There are 0 best solutions below