// Debug const { DebugBuilder } = require("../utilities/debugBuilder.js"); const log = new DebugBuilder("client", "httpRequests"); // Config require('dotenv').config(); // Modules const http = require("http"); var runningClientConfig = new nodeObject({_id: process.env.CLIENT_ID, _ip: process.env.CLIENT_IP, _name: process.env.CLIENT_NAME, _port: process.env.CLIENT_PORT, _location: process.env.CLIENT_LOCATION, _nearbySystems: process.env.CLIENT_NEARBY_SYSTEMS, _online: process.env.CLIENT_ONLINE}); exports.requestOptions = class requestOptions { constructor(path, method, hostname = undefined, headers = undefined, port = undefined) { if (method === "POST"){ this.hostname = hostname ?? runningClientConfig.hostname this.path = path this.port = port ?? runningClientConfig.port this.method = method this.headers = headers ?? { 'Content-Type': 'application/json', } } } } /** * Send the HTTP request to the server * @param requestOptions * @param data * @param callback */ exports.sendHttpRequest = function sendHttpRequest(requestOptions, data, callback){ log.DEBUG("Sending a request to: ", requestOptions.hostname, requestOptions.port) // Create the request const req = http.request(requestOptions, res => { res.on('data', (data) => { const responseObject = { "statusCode": res.statusCode, "body": JSON.parse(data) }; log.DEBUG("Response Object: ", responseObject); callback(responseObject); }) }).on('error', err => { log.ERROR('Error: ', err.message) // TODO need to handle if the server is down }) // Write the data to the request and send it req.write(data) req.end() }