split out got request
This commit is contained in:
parent
65f894c3d1
commit
67eca698b8
52
app.js
52
app.js
@ -1,4 +1,3 @@
|
|||||||
const got = require('got')
|
|
||||||
fs = require('fs');
|
fs = require('fs');
|
||||||
var tableify = require('tableify');
|
var tableify = require('tableify');
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
@ -9,24 +8,7 @@ const port = 6969
|
|||||||
let DNSArray = require('./DNSArray.json')
|
let DNSArray = require('./DNSArray.json')
|
||||||
let streamArrays = require('./streamArray.json')
|
let streamArrays = require('./streamArray.json')
|
||||||
|
|
||||||
const gotRequest = async (url) => {
|
const { gotRequest } = require('./gotRequest')
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
function writeFile(streamArrays) {
|
function writeFile(streamArrays) {
|
||||||
fs.writeFileSync('streamArray.json', JSON.stringify(streamArrays), function (err) {
|
fs.writeFileSync('streamArray.json', JSON.stringify(streamArrays), function (err) {
|
||||||
@ -71,24 +53,18 @@ function arrayToTable(array) {
|
|||||||
return tableify(array)
|
return tableify(array)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function server() {
|
app.use(basicAuth({
|
||||||
|
users: { 'BBLBTV': 'BBLBTV' },
|
||||||
|
challenge: true,
|
||||||
|
realm: 'foo',
|
||||||
|
}))
|
||||||
|
|
||||||
app.use(basicAuth({
|
app.get('/', async (req, res) => {
|
||||||
users: { 'BBLBTV': 'BBLBTV' },
|
let fullStreamArray = await main()
|
||||||
challenge: true,
|
let fullStreamHTML = await arrayToTable(fullStreamArray)
|
||||||
realm: 'foo',
|
res.send(fullStreamHTML)
|
||||||
}))
|
})
|
||||||
|
|
||||||
app.get('/', async (req, res) => {
|
app.listen(port, () => {
|
||||||
let fullStreamArray = await main()
|
console.log(`DNS Logger listening at http://localhost:${port}`)
|
||||||
let fullStreamHTML = await arrayToTable(fullStreamArray)
|
})
|
||||||
res.send(fullStreamHTML)
|
|
||||||
})
|
|
||||||
|
|
||||||
app.listen(port, () => {
|
|
||||||
console.log(`DNS Logger listening at http://localhost:${port}`)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
server()
|
|
||||||
|
23
gotRequest.js
Normal file
23
gotRequest.js
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user