Created
March 28, 2017 08:34
-
-
Save nojaf/46ea1e5c33de7f4ae1d4a5d52dd791f2 to your computer and use it in GitHub Desktop.
Base64 images with express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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