Reputation: 494
I'm trying to Fetch a URL (http://localhost) that will return a picture (At this moment, the extension doesn't matter) through HTTP using Node.js.
Front End
let image = await fetch('http://localhost:3031', {
mode: 'cors',
method: 'GET'
})
Back End
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
res.setHeader('Content-Type', 'image/png');
fs.readFile('image.png', (err, data) => {
res.write(data, "binary");
res.end();
});
}).listen(3031)
I want to take that picture and then display it into the Website.
Im getting the file, NOT THE SRC
Upvotes: 2
Views: 179
Reputation: 16140
Directly in the HTML as:
<img id="loadedimage" src="http://localhost:3031/"/>
Or with fetch
, using createObjectURL
:
var element = document.getElementById('loadedimage');
var response = await fetch('http://localhost:3031');
var image = await response.blob();
element.src = URL.createObjectURL(image);
Working demo: https://codepen.io/bortao/pen/oNXpvYR
Upvotes: 2