Skip to content

Instantly share code, notes, and snippets.

@hscheuerle
Last active October 27, 2018 14:34
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