Skip to content

Instantly share code, notes, and snippets.

@madhums
Forked from hackable/expressjs-base64-image.js
Created March 9, 2012 09:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madhums/2005734 to your computer and use it in GitHub Desktop.
Save madhums/2005734 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment