I've been trying to fetch multiple images through URLs in the server and try to display them in the browser. When I click the run button, I know images have been fetched by the server through console log (which shows the two buffer items), but nothing shows up in the browser.
I understand that I can fetch directly from the browser, but eventually I'll have to fetch from the URLs saved in the database, so ideally I could fetch from the server, and display the images in the browser.
Below is the code.
Any help or insights would be greatly appreciated!
Browser:
console.log('running')
var outside
document.getElementById('getLogos').addEventListener('click',getLogos);
function getLogos(){
fetch('http://localhost:8080/logos'
)
// .then((res) => res.blob())
.then((data) => {
let imagesInfo = JSON.parse(data);
let pics = "";
for (i=0;i<imagesInfo.length;i++){
// document.write("<li><img src='" + images[i] + "' width='160' height='120'/><span>" + images[i] + "</span></li>");
pics += `
<li><img src='" + images[i] + "' width='160' height='120'/><span>" + images[i] + "</span></li>
`
}
// outside = URL.createObjectURL(images)
document.getElementById('output').innerHTML = pics;
console.log(images)
console.log(imagesInfo)
})
}
<!DOCTYPE html>
<html>
<body>
<h1>Logos</h1>
<ul id="logos">
<button id="getLogos">Get Logos</button>
</ul>
<div id="output"></div>
</body>
</html>
Server - node
console.log('server is starting');
const express = require('express');
const morgan = require('morgan');
const app = express();
//const mysql = require('mysql');
const request = require('request');
app.use(express.static('./public'));
app.listen(8080, () => {
console.log("Listening on 8080")
})
app.use(morgan('short'));
app.get("/logos", (req, res, next) => {
var options = [
{
url: "https://www.google.com/images/srpr/logo11w.png", ///url will be sourced from a database
method: 'GET',
headers: {'Content-Type': 'image/png'},
encoding: null
},
{
url: "https://logos-download.com/wp-content/uploads/2016/02/Walmart_logo_transparent_png.png",
method: 'GET',
headers: {'Content-Type': 'image/png'},
encoding: null
}
]
var result = [];
options.forEach(function (option) {
request(option, function (err, body) {
if (err) {
console.log(err)
return;
} else
// console.log(body.body)
result.push(body.body);
console.log(result)
resultString = JSON.stringify(result)
res.write (resultString);
res.end
})
})
})