I need help: I'm running express in node JS and I'm trying to pass an image through an HTTP request and or response
Express initialization:
var app = express();
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.text({type:'text/plain'}));
Http request (ajax inside index.html):
var request = new XMLHttpRequest();
request.open('GET', '/request');
request.setRequestHeader('Content-Type', 'text/plain');
request.onreadystatechange = function() {
if(request.readyState == 4){
//do stuff with request.responseText
}
};
request.send('request for image xxxx');
Express handling the request:
app.get('/request', (req, res) => {
if(req.body == 'request for image xxxx')
res.status(200).send(???);
//other stuff
});
My middle-ware 'bodyParser' does support other data types if they are url-encoded, but I have no idea how to do that, so I'm forced to only use text/plain messages, except there comes a problem when I want to send an image over.
I'm guessing I could probably do that using buffers maybe, but I have absolutely no idea what the syntax for that would be and the HTTP handling syntax I'm already using is probably extremely over-complicated or something. I'm quite new to HTTP protocols.