url-shorten
A url-shortener for S3, mongodb and redis.
The MongoDB and REDIS versions are mostly there for the sake of completeness (there are other libraries that do that) but the interesting part is the S3 version. This uses the capability of S3 to do redirection so no active service needs to be on the path for the redirection to work. In particular, this works with cloudfront too which makes the redirection quite fast.
Install
npm install url-shorten
API
The default implementation requires a mongodb instance to support an auto-increment counter. The generated short urls are basically base-36 encoded versions of this counter and so will only use lower case characters and numbers -- no other special characters at all.
Basic Usage
var config = mongoUrl: 'mongodb://user:pass@host:port/database'; configs3key = '<your S3 key>'; configs3secret = '<your S3 secret>'; configs3bucket = '<your S3 bucket>'; configshortUrlPrefix = 'http://mysite.com.s3-website-us-east-1.amazonaws.com/'; configuniqueIdPrefix = 'shortUrls-'; // all short urls will have this suffixed to shortUrlPrefix var shortener = config; shortener; // you can find out the long URL for a short url via unshorten shortener
Changing counter implementation
This uses mongodb-counter as the default implementation for the unique id generator. You can pass your own unique ID generator as follows:
config { ... ; ... } var shortener = config;
In particular, this does not use the fast unique id generation that is provided by default in the mongodb-counter package but it is trivial to override and pass that instead.
Also, if you would rather not use mongodb but use redis instead for the counter, you can use the redis-counter package:
var redisCounter = ; configcounters = redisCounter; var shortener = config;
Using MongoDB or REDIS to store the mappings
If you do not want to use S3, you can store the shortUrl to longUrl mapping in mongodb or redis.
configstore = ; var shortener = config; // or for redis configstore =