Skip to content

Instantly share code, notes, and snippets.

@ivandez
Created April 5, 2023 13:51
Show Gist options
  • Save ivandez/5d547f5bedd0a3bf5312e37e359feaa9 to your computer and use it in GitHub Desktop.
Save ivandez/5d547f5bedd0a3bf5312e37e359feaa9 to your computer and use it in GitHub Desktop.
Q9 solution
// Importing
const fs = require("fs");
const https = require("https");
// INSTRUCTIONS TO RUN THE TEST
// Dear tester, just replace the bellow value with the path of the file you want to test
// and everything will work
const testFilePath = "url.txt"; // REPLACE ME!
// Classes
class ReadFile {
filePath;
constructor(filePath) {
this.filePath = filePath;
}
read() {
try {
const fileContent = fs
.readFileSync(this.filePath)
.toString("UTF8")
.split("\n");
const contentWithReplaceNewLine = fileContent.map((item) =>
item.replace("\r", "")
);
return contentWithReplaceNewLine;
} catch (error) {
console.log(
"Error while triying to execute ReadFile.read(), error code:",
error.code
);
return error.code;
}
}
}
class HttpsRequest {
getRequest(url) {
https
.get(url, ({ statusCode }) => {
console.log(`Url: ${url}`);
console.log(`Status Code: ${statusCode}`);
console.log("--------------------");
})
.on("error", (e) => {
console.error(
`Error while triying to request to ${url} error code: ${e.code}`
);
console.log("--------------------");
});
}
}
const readFile = new ReadFile(testFilePath);
const httpsRequest = new HttpsRequest(https);
try {
const urls = readFile.read();
urls.forEach((url) => {
httpsRequest.getRequest(url);
});
} catch (error) {
console.log("Error on main statement", error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment