Skip to content

Instantly share code, notes, and snippets.

@njoubert
Forked from erichocean/gist:799195
Last active December 19, 2015 14:59
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 njoubert/5973325 to your computer and use it in GitHub Desktop.
Save njoubert/5973325 to your computer and use it in GitHub Desktop.
Small webservice that converts the given url path to a data-uri. Run it, call http://localhost:3010/?url=http://SOME_PATH/ updated for request 2.x.x and express 3.x.x
var express = require('express'),
request = require('request'),
sys = require('sys');
var app = express();
app.use(express.logger());
app.use(express.bodyParser());
app.get('/', function(req, res){
if(req.param("url")) {
var url = unescape(req.param("url"));
console.log("Incoming stuff:", url)
request({uri:url}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data_uri_prefix = "data:" + response.headers["content-type"] + ";base64,";
var image = new Buffer(body.toString(), 'binary').toString('base64');
image = data_uri_prefix + image;
res.send('<img src="'+image+'"/>');
}
});
}
});
app.listen(3010);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment