Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function apiController(req, res){
- const response = await request.get('http://...........)
- res.status(200).json({...})
- app.get('/myurl', function (req, res) {
- makeApiRequest().then(result => {
- // We reach here after we are all done with the API.
- res.send('here is a response to browser using data from result');
- });
- });
- async function makeApiRequest() {
- // here, make the API request, do a polling loop, etc.
- // eventually return the result
- }
- - Browser client makes JSON request to your server.
- - Server makes API call to service, gets back a 'request id'.
- - Server sends back request id to browser client.
- - Browser client enters loop, every 5 seconds, it sends server a message.
- - 'Did my request id complete yet?'
- - Server takes request id, asks service 'what is the answer?'
- - Send response back to browser.
- - Browser client loops until the request is complete, then continues.
Add Comment
Please, Sign In to add comment