Make working API

main
Elnu 2 years ago
parent cdd6b1221a
commit c9ddb2563e

@ -0,0 +1,27 @@
const app = require("express")();
app.use(require("cors")());
const fetch = require("node-fetch");
const jsdom = require("jsdom");
app.get("/", (request, response) => {
response.writeHead(200, { "Content-Type": "application/json" });
fetch("https://www.bing.com/images/search?q=" + request.url.substring(request.url.indexOf("?") + 1))
.then(response => response.text())
.then(text => new jsdom.JSDOM(text).window.document)
.then(document => {
const imgNodes = document.getElementsByClassName("mimg");
let results = [];
for (let i = 0; i < imgNodes.length; i++) {
const src = imgNodes[i].src;
if (!src.startsWith("http")) {
break;
}
results.push(src);
}
response.write(JSON.stringify(results));
response.end();
});
});
app.listen(process.argv[2] ? process.argv[2] : 3000);