Skip to content

Instantly share code, notes, and snippets.

@nojaf
Created March 28, 2017 08:34
Show Gist options
  • Select an option

  • Save nojaf/46ea1e5c33de7f4ae1d4a5d52dd791f2 to your computer and use it in GitHub Desktop.

Select an option

Save nojaf/46ea1e5c33de7f4ae1d4a5d52dd791f2 to your computer and use it in GitHub Desktop.
Base64 images with express
const express = require("express");
const app = express();
const fs = require("fs");
function base64_encode(file) {
// read binary data
return new Promise((resolve, reject) => {
fs.readFile(file, (err, data) => {
if(err){
reject(err);
}
resolve(new Buffer(data).toString('base64'));
})
});
}
app.get('/', function(request, response) {
base64_encode("image.jpg").then(base64 => {
response.send(`<html>
<head></head>
<body>
<img src="data:image/jpg;base64,${base64}" />
</body>
</html>`);
}).catch(err => {
response.send(err);
});
});
app.listen(9527, function() {
console.log("express launched");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment