Skip to content

Instantly share code, notes, and snippets.

@maxsinev
Created July 19, 2018 09:42
Show Gist options
  • Save maxsinev/139bd1131f9e88b52b71dfb0e52d08b0 to your computer and use it in GitHub Desktop.
Save maxsinev/139bd1131f9e88b52b71dfb0e52d08b0 to your computer and use it in GitHub Desktop.
Return server-loaded image as Buffer to client
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<img src="/image">
</body>
</html>
var express = require('express');
var app = express();
var request = require('request');
var path = require('path');
app.use(express.static(__dirname));
app.get('/', function(req, res) {
res.sendFile(path.join(public, 'index.html'));
});
app.get('/image', function (req, res) {
var options = {
method: 'GET',
url: 'https://i.stack.imgur.com/Iyh5S.png',
// body is Buffer
encoding: null
}
//https://i.stack.imgur.com/Iyh5S.png
request(options, (err, resp, body) => {
res.writeHead(200, {
'Content-Type': resp.headers['content-type'],
'Content-Length': body.length
});
res.end(body);
});
});
app.listen(3000, function () {
console.log('Server running...');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment