Skip to content

Commit

Permalink
Check http statusCode
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Nov 28, 2018
1 parent fe7e172 commit fe82548
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ const getIndexContent = (options, callback) => {
const getIndexTemplate = (options, callback) => {
try {
const url = `${options.root}/${templateName}`
httpGet(url, callback)
} catch (err) {
callback(err)
}
}

const httpGet = (url, callback) => {
try {
const isHttps = new URL(url).protocol === 'https:'
const get = isHttps ? https.get : http.get
get(url, resp => {
get(url, res => {
const { statusCode } = res
if (statusCode !== 200) callback(new Error(`Status Code ${statusCode}`))
let data = ''
resp.on('data', chunk => { data += chunk })
resp.on('end', () => { callback(null, data) })
resp.on('err', callback)
res.on('data', chunk => { data += chunk })
res.on('end', () => { callback(null, data) })
res.on('err', callback)
})
} catch (err) {
callback(err)
Expand Down

0 comments on commit fe82548

Please sign in to comment.