Last active
October 27, 2018 14:34
node.js http request with redirect
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 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