简体   繁体   中英

How to pass final response in 2 following request by axios

User makes an http request to server 1 and pass a json file. ->Then, server 1 runs http request to server 2 with the json file (while send response to user 200/timeout etc...) -> server 2 returns value -> value return to server 1... The question is: how to pass the value to user?

I can not find a way to pass the final value.

USER TO SERVER 1:

axios({
       method: 'post',
       url: Url,
       headers: {
       "Content-Type": "application/json"
       },
       timeout: 30000, // Wait for 30 seconds
       data: data
       }).then(function(response){
       console.log(response)
       }).catch(function(error) {
       console.log(error)
      })

SERVER 1 TO SERVER 2:

app.use('/payment', (req, res)=> {
      var data = req.body
      data.CreditboxToken = creditToken
      axios({
            method: 'post',
            url: urlPayment,
            headers: {
              "Content-Type": "application/json"
            },
            timeout: 30000, // Wait for 30 seconds
            data: data
          })
          .then(function(response){
            console.log(response.data)
            res.send(response.data)
          })
          .catch(function(error) {
            console.log(error)
          });
})

What you have in your example looks like an approach that could work. The axios call on server 1 creates and executes the call to server 2 and only when server 2 succeeds does it then do "res.send" to the user.

You don't mention the actual behavior that you are seeing here, what are your console.logs outputting?

Note: If the http request from the user to server1 has been completed (ie server1 responded or the connection timed out) then the context of that specific request is closed and finished. You cannot then return your response from server 2 to the user without some other request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM