diff --git a/lib/content.js b/lib/content.js index 996ef18..ed08c9a 100644 --- a/lib/content.js +++ b/lib/content.js @@ -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)