add headers and status to response

This commit is contained in:
Arthur Lu 2023-01-22 23:17:38 +00:00
parent 73860cbb2c
commit 49e44d776d

View File

@ -6,6 +6,7 @@ const helmet = require("helmet");
const morgan = require("morgan"); const morgan = require("morgan");
const https = require("https"); const https = require("https");
var package = require("./package.json"); var package = require("./package.json");
const { request } = require("http");
const app = express(); const app = express();
app.use(helmet()); app.use(helmet());
@ -56,20 +57,37 @@ function requestPVE (path, method, cookies, callback, body = null, auth = true)
content.body = prms.toString(); content.body = prms.toString();
content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken; content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken;
} }
https.request(content, (res) => {
let data = ""; const promiseResponse = new Promise((resolve, reject) => {
res.on("data", (d) => { const fullResponse = {
data += d.toString(); status: '',
body: '',
headers: ''
};
const request = https.request(content);
request.on('error', reject);
request.on('response', response => {
response.setEncoding('utf8');
fullResponse.status = response.statusCode;
fullResponse.headers = response.headers;
response.on('data', chunk => { fullResponse.body += chunk; });
response.on('end', () => {
if(fullResponse.body){
fullResponse.body = JSON.parse(fullResponse.body);
}
resolve(fullResponse);
});
}); });
res.on("end", () => {
try{ request.end();
callback(JSON.parse(data)); });
}
catch (e) { promiseResponse.then(
callback(e); response => {callback(response);},
} error => {callback(error);}
}); );
}).on("error", (e) => {callback(e)}).end();
} }
app.listen(80, () => { app.listen(80, () => {