From 67eca698b8f2a793b510760a0682447ed1a9da89 Mon Sep 17 00:00:00 2001 From: Karl Date: Sat, 6 Feb 2021 11:19:15 +0000 Subject: [PATCH] split out got request --- app.js | 52 ++++++++++++++------------------------------------- gotRequest.js | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 38 deletions(-) create mode 100644 gotRequest.js diff --git a/app.js b/app.js index b6a0fdd..7ba6072 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,3 @@ -const got = require('got') fs = require('fs'); var tableify = require('tableify'); const express = require('express') @@ -9,24 +8,7 @@ const port = 6969 let DNSArray = require('./DNSArray.json') let streamArrays = require('./streamArray.json') -const gotRequest = async (url) => { - - let returnResponse = {}; - let options = {} - await got(url, options) - .then((response) => { - returnResponse = response; - }) - .catch((error) => { - returnResponse = typeof error.response !== 'undefined' ? error.response : error; - - if (typeof returnResponse.body === 'string' && returnResponse.body.substring(0, 1) === '{') { - returnResponse.body = JSON.parse(returnResponse.body); - } - }); - - return returnResponse; -}; +const { gotRequest } = require('./gotRequest') function writeFile(streamArrays) { fs.writeFileSync('streamArray.json', JSON.stringify(streamArrays), function (err) { @@ -71,24 +53,18 @@ function arrayToTable(array) { return tableify(array) } -async function server() { +app.use(basicAuth({ + users: { 'BBLBTV': 'BBLBTV' }, + challenge: true, + realm: 'foo', +})) - app.use(basicAuth({ - users: { 'BBLBTV': 'BBLBTV' }, - challenge: true, - realm: 'foo', - })) +app.get('/', async (req, res) => { + let fullStreamArray = await main() + let fullStreamHTML = await arrayToTable(fullStreamArray) + res.send(fullStreamHTML) +}) - app.get('/', async (req, res) => { - let fullStreamArray = await main() - let fullStreamHTML = await arrayToTable(fullStreamArray) - res.send(fullStreamHTML) - }) - - app.listen(port, () => { - console.log(`DNS Logger listening at http://localhost:${port}`) - }) -} - - -server() \ No newline at end of file +app.listen(port, () => { + console.log(`DNS Logger listening at http://localhost:${port}`) +}) diff --git a/gotRequest.js b/gotRequest.js new file mode 100644 index 0000000..956d2e8 --- /dev/null +++ b/gotRequest.js @@ -0,0 +1,23 @@ +const got = require('got') + +const gotRequest = async (url) => { + let returnResponse = {}; + let options = {} + await got(url, options) + .then((response) => { + returnResponse = response; + }) + .catch((error) => { + returnResponse = typeof error.response !== 'undefined' ? error.response : error; + + if (typeof returnResponse.body === 'string' && returnResponse.body.substring(0, 1) === '{') { + returnResponse.body = JSON.parse(returnResponse.body); + } + }); + + return returnResponse; +}; + +module.exports = { + gotRequest +} \ No newline at end of file