Giter Site home page Giter Site logo

jedahan / short Goto Github PK

View Code? Open in Web Editor NEW

This project forked from edwardhotchkiss/short

0.0 2.0 0.0 757 KB

Node.JS URL Shortener backed by MongooseJS w/ Examples

Home Page: http://edwardhotchkiss.github.com/short/

License: MIT License

JavaScript 100.00%

short's Introduction

short Build Status

Node.JS URL Shortener backed by Mongoose.JS w/ Examples

Notes migrating to 1.0.0+

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+

Running Examples

$ git clone [email protected]:edwardhotchkiss/short.git
$ cd short/examples/basic
$ node api.js

Installation

$ npm install short

Basic API Usage

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);
      }
    });
  }
});

Complete Example with Express

Please see https://github.com/thinkroth/shortUrl

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, Edward Hotchkiss.

Contributors:

Kevin Roth, Chase Brammer, Chris Lynch,

short's People

Contributors

edwardhotchkiss avatar thinkroth avatar cbrammer avatar lynchseattle avatar

Watchers

James Cloos avatar  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.