Git Product home page Git Product logo

short's Introduction

short Build Status Dependency Status

NPM

Node.js URL Shortener backed by Mongoose.js

No Callbacks, just Promises!

Installation

$ npm install short

Basic API Usage

Generates a Shortened URL Doc, then retrieves it for demo:

var shortURLPromise
  , short = require('../lib/short');

// connect to mongodb
short.connect('mongodb://localhost/short');

short.connection.on('error', function(error) {
  throw new Error(error);
});

// promise to generate a shortened URL.
var shortURLPromise = short.generate({
  URL : 'http://nodejs.org/'
});

// gets back the short url document, and then retrieves it
shortURLPromise.then(function(mongodbDoc) {
  console.log('>> created short URL:');
  console.log(mongodbDoc);
  console.log('>> retrieving short URL: %s', mongodbDoc.hash);
  short.retrieve(mongodbDoc.hash).then(function(result) {
    console.log('>> retrieve result:');
    console.log(result);
    process.exit(0);
  }, function(error) {
    if (error) {
      throw new Error(error);
    }
  });
}, function(error) {
  if (error) {
    throw new Error(error);
  }
});

Listing all Shortened URLs in DB:

var listURLsPromise
  , short = require('../lib/short');

// connect to mongodb
short.connect('mongodb://localhost/short');

short.connection.on('error', function(error) {
  throw new Error(error);
});

// promise to retrieve all shortened URLs
listURLsPromise = short.list();

// output all resulting shortened url db docs
listURLsPromise.then(function(URLsDocument) {
  console.log('>> listing (%d) Shortened URLS:', URLsDocument.length);
  console.log(URLsDocument);
  process.exit(0);
}, function(error) {
  if (error) {
    throw new Error(error);
  }
});

Updating the URL or the data fields of an existing Short URL hash

// Basically, update works like this
var updatePromise = short.update(hash, updateData);
// hash => Short url hashcode generated using short.generate()
// updateData => An object consisting of the new URL and/or the new data object. 
//               If a key already exists in the current data object, it's value is updated, 
//               otherwise, it is added and saved to the data object

//This function returns a promise which on resolution returns the new updated object as an argument.

Here's some working code. hash is assumed to be given

// The basic Initialisation, Connection, Short URL generation and 
// retrieval remains the same as depicted in previous examples

// the variable hash contains the short url hash code generated using short.generate()
var updatePromise = short.update(hash,{
  URL : 'http://www.youtube.com/watch?v=qvsgGtivCgs',
  data: {
    'type' : 'movie-trailer',
    'movie': 'Back To The Future'
  }
});
updatePromise.then(function(ShortURLObject) {
  console.log('New URL:', ShortURLObject.URL, '\nNew data:', ShortURLObject.data);
}, function(error) {
  console.log('Error', error);
});

Contribute

  1. Fork
  2. Clone forked repository
  3. Add some sweet code
  4. Tests still passing? Run tests with npm test
  5. Add a test if adding a feature
  6. Pull Request
  7. Instant Karma!

License (MIT)

Copyright (c) 2011-2013, Edward Hotchkiss.

Bitdeli Badge

short's People

Contributors

edwardhotchkiss avatar thinkroth avatar t27 avatar matmar10 avatar cbrammer avatar lynchseattle avatar romanmt avatar bitdeli-chef avatar jperkelens avatar lbj96347 avatar

Watchers

James Cloos avatar Yudhajit Sinha avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.