Node.JS URL Shortener backed by Mongoose.JS w/ Examples
NOTE: If you've used any version of Short before 1.0.0, please note the new API. This is a finalized API & with the previous API structure being incompatible with Short 1.0.0+
$ git clone git@github.com:edwardhotchkiss/short.git
$ cd short/examples/basic
$ node api.js
$ npm install short
Using short within your own project as an API interface
/**
* @list dependencies
**/
var short = require('short')
, URL = 'http://nodejs.org/';
/**
* @description connect to mongodb
**/
short.connect('mongodb://localhost/short');
short.connection.on('error', function(error){
throw new Error(error);
});
/**
* @description generate a shortened URL
* ... Retrieve URL based on Generated Hash
**/
short.generate(URL, function(error, shortURL) {
if (error) {
throw new Error(error);
} else {
short.retrieve(shortURL.hash, function(error, shortenedURLObject) {
if (error) {
throw new Error(error);
} else {
console.log('URL:', shortenedURLObject.URL);
console.log('hash:', shortenedURLObject.hash);
process.exit(0);
}
});
}
});
/**
* @description Generate a Shortened URL with custom data stored on the hashed URL
* Retrieve URL based on Generated Hash and retrieve custom data
* Make sure to use obj.data.toObject() for accessing your custom data
**/
var short = require('short')
, URL = 'http://nodejs.org/',
, options = {length: 6, data: {'my':'value','is':2}};
short.generate(URL, options, function(error, shortURL) {
if (error) {
throw new Error(error);
} else {
short.retrieve(shortURL.hash, function(error, shortenedURLObject) {
if (error) {
throw new Error(error);
} else {
console.log('URL:', shortenedURLObject.URL);
console.log('hash:', shortenedURLObject.hash);
console.log('data:', JSON.stringify(shortenedURLObject.data.toObject()));
process.exit(0);
}
});
}
});
Please see https://github.com/thinkroth/shortUrl
- Fork
- Clone forked repository
- Add some sweet code
- Tests still passing? Run tests with
npm test
- Add a test if adding a feature
- Pull Request
- Instant Karma!
Copyright (c) 2011, Edward Hotchkiss.