mongoose-url-shortener
v1.0.1
Published
mongoose-url-shortener
Downloads
5
Readme
###MongooseURLShortener
Please Share on Twitter if you like #MongooseURLShortener
A simple URL Shortening library for NodeJS using Promises/A+ results.
####Installation
npm install mongoose-url-shortener --save
####API
//Setup Mongoose
var connection = require('mongoose').connect('mongodb:testing');
//Initialize Shortener
var urlShotener = MongooseURLShortener(connection, options);
#####Options { seed:12345, schema: { customSchemaValue:Number, customSchemaObj:{} } } Seed Optional value to use for generating short url's. You must always use the same seed otherwise you will not be able to resolve short urls' back to their original url.
Schema Optional schema properties for the Mongoose object. You can then pass the extra values you define here to be saved along with the URL when shortening or resolving
#####Shorten Shorten will either create a new short url in your database or resolve to a previously shortened URL.
url = 'http://www.google.com';
data = { 'any data you want to record with the short url' : true }
var promise = urlShortener.shorten(url, data);
promise.then(function(url){
console.log(url.hash);
//aGasjn1Ho
}).fail(function(err){
console.error('Error creating short url', err);
});
#####Resolve Resolve will return the original URL before shortening if one is available in the database.
url = 'aGasjn1Ho';
data = {ip:'127.0.0.1'};
var result = urlShotener.resolve(url, data);
result.then(function(url){
console.log(url );
// {
// type: 'MongooseURLShortener',
// url: 'http://www.google.com',
// hash:'aGasjn1Ho',
// hits:{
// {ip:'127.0.0.1},
// {ip:'127.0.0.1},
// },
// totalHits: 2
//}
}).fail(function(err){});
As you can see each time you resolve a url your data passed to the object will be added to an array of hits that way you can record data or analytics each time the url is accessed.
Check out NodeTinyUrl for a working implementation
Please Share on Twitter if you like #MongooseURLShortener