Skip to content

Commit fe82548

Browse files
committed
Check http statusCode
1 parent fe7e172 commit fe82548

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/content.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,23 @@ const getIndexContent = (options, callback) => {
2323
const getIndexTemplate = (options, callback) => {
2424
try {
2525
const url = `${options.root}/${templateName}`
26+
httpGet(url, callback)
27+
} catch (err) {
28+
callback(err)
29+
}
30+
}
31+
32+
const httpGet = (url, callback) => {
33+
try {
2634
const isHttps = new URL(url).protocol === 'https:'
2735
const get = isHttps ? https.get : http.get
28-
get(url, resp => {
36+
get(url, res => {
37+
const { statusCode } = res
38+
if (statusCode !== 200) callback(new Error(`Status Code ${statusCode}`))
2939
let data = ''
30-
resp.on('data', chunk => { data += chunk })
31-
resp.on('end', () => { callback(null, data) })
32-
resp.on('err', callback)
40+
res.on('data', chunk => { data += chunk })
41+
res.on('end', () => { callback(null, data) })
42+
res.on('err', callback)
3343
})
3444
} catch (err) {
3545
callback(err)

0 commit comments

Comments
 (0)