Skip to content

Instantly share code, notes, and snippets.

@hackable
Created October 18, 2011 05:21
Show Gist options
  • Save hackable/1294667 to your computer and use it in GitHub Desktop.
Save hackable/1294667 to your computer and use it in GitHub Desktop.
Node.js base64 encode a downloaded image for use in data URI
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
@renatoargh
Copy link

thank you!!!

@nanuclickity
Copy link

Just what i was looking for. 👍

@nwohaibi
Copy link

nwohaibi commented Mar 5, 2015

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment