Created
October 18, 2011 05:21
-
-
Save hackable/1294667 to your computer and use it in GitHub Desktop.
Node.js base64 encode a downloaded image for use in data URI
This file contains 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
express = require("express") | |
request = require("request") | |
BufferList = require("bufferlist").BufferList | |
app = express.createServer(express.logger(), express.bodyParser()) | |
app.get "/", (req, res) -> | |
if req.param("url") | |
url = unescape(req.param("url")) | |
request | |
uri: url | |
encoding: 'binary' | |
, (error, response, body) -> | |
if not error and response.statusCode is 200 | |
data_uri_prefix = "data:" + response.headers["content-type"] + ";base64," | |
image = new Buffer(body.toString(), "binary").toString("base64") | |
image = data_uri_prefix + image | |
res.send "<img src=\"" + image + "\"/>" | |
app.listen 3000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks