From fe82548367ab1caa209e73a2e7d2e1fe7caf3bce Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Wed, 28 Nov 2018 10:22:16 -0800 Subject: [PATCH] Check http statusCode --- lib/content.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) 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)