Last active
May 28, 2017 07:15
-
-
Save coco98/5dfb00e75ef132a9a6c58644928a4eed to your computer and use it in GitHub Desktop.
Nodejs service on Hasura making an API request to another API on Hasura
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('isomorphic-fetch'); | |
const express = require('express'); | |
const app = express(); | |
//your routes here | |
app.get('/hello', function (req, res) { | |
const url = 'http://api2.default/respond'; | |
const options = { | |
method: 'GET' | |
}; | |
fetch(url, options).then( | |
(response) => { | |
if (response.ok) { | |
response.text().then( | |
(t) => { | |
// Send the response received from api2 | |
res.send(t); | |
}); | |
} else { | |
res.status(500) | |
.send('Error from api2: ' + response.status.toString()); | |
} | |
}, | |
(error) => { | |
console.error(error); | |
res.status(500).send('Internal error contacting api2'); | |
}); | |
}); | |
app.listen(8080, function () { | |
console.log('Example app listening on port 8080!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment