Tuesday, July 16, 2013

Base-64 encoding images in Node.js

First, create a new file, app.js, and add the following code:


#!/usr/bin/env node

var fs = require("fs"),
    util = require("util");

var mime = require("mime");

var dataUri = base64Image("./icon4.png");
console.log(dataUri);

function base64Image(src) {
    var data = fs.readFileSync(src).toString("base64");
    return util.format("data:%s;base64,%s", mime.lookup(src), data);

}