Skip to content

Instantly share code, notes, and snippets.

@hscheuerle
Last active October 27, 2018 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hscheuerle/f9fa454edd91cb82d56b279860a77835 to your computer and use it in GitHub Desktop.
Save hscheuerle/f9fa454edd91cb82d56b279860a77835 to your computer and use it in GitHub Desktop.
node.js http request with redirect
const handleRequest = async uri => {
return new Promise((resolve, reject) => {
http.get(uri, async res => {
const { statusCode } = res
if (statusCode === 302) { // redirect
const { location } = res.headers
const data = await handleRequest(location)
resolve(data)
} else if (statusCode === 200) { // success
const data = await handleData(res)
resolve(data)
} else {
reject(`${statusCode} not handled`)
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment