A redis and MongoDB backed URL-shortener which only generates 1 short-url per url. In a cluster setup it can be used with a central mongodb or mongodb replicatset and a redis on each client facing server for caching.
The mongoDB is used as permanent datastore. The Redis is used for caching.
$ npm install unique-shortener --save
var MongoClient, UniqueShortener, config, redis, shortener,
redis = require('redis');
MongoClient = require('mongodb').MongoClient;
UniqueShortener = require("unique-shortener");
config = {
validation: false
};
shortener = new UniqueShortener(config);
redis = redis.createClient();
redis.select(0);
MongoClient.connect("mongodb://127.0.0.1:27017/unique-shortener", function(err, mdb) {
if (err != null) {
console.error(err);
} else {
shortener.init(mdb, redis);
shortener.shorten('http://www.valiton.com', function(err, result) {
console.log(JSON.stringify(result));
});
}
});
validation: defines if the input string should be validated as a valid url
The shortener uses mongodb for primary data store and redis for caching. The init-Method requires a mongodb client connection and a standard redis client. the callback is optional
Returns a result object with short-key and flag if the url was already existed or an error-object.
Example:
shortener.shorten url, (err, result) ->
console.log(result.key)
console.log(result.createdNew)
Result object:
{
key:"oisdjf9",
"createdNew": true
}
Returns the resolved url if it exists.
Example:
shortener.resolve key, (err, url) ->
console.log(url)
Returns NotFound error if the key wasn't found
$ git clone https://github.com/valiton/node-unique-shortener.git
$ npm install
runs grunt prod task in background
$ npm test
- use farmhash instead of cityhash (cityhash will not work on [email protected])
- Bugfixes
- update Documentation
- add grunt dev task for auotreload of tests
- add travis
- Added index to MongoDb
- rebuild shortening with hashing
- created version with mongo as primary storage and redis as mongo cache
- Initial version
- Gleb Kotov
- Alexander Stautner
- Benedikt Weiner
Copyright (c) 2013 Valiton GmbH Licensed under the MIT license.