Code Monkey home page Code Monkey logo

short's Introduction

Short Build Status

NodeJS URL Shortener backed by MongooseJS 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

var short = require('../../lib/short'),
    // URL to Shorten
    URL = 'http://nodejs.org/';

/*!
  Connect to MongoDB w/ MongooseJS
 */

short.connect(MONGO_DB_SHORT);

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

/*!
  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 {
        // URL
        console.log('URL:', shortenedURLObject.URL);
        // Base 62 Hash
        console.log('hash:', shortenedURLObject.hash);
        process.exit(0);
      };
    });
  }
});

Complete Example with Express

var url = require('url'),
    express = require('express'),
    short = require('short'),
    app = express.createServer(),
    port = process.env.PORT || 8000,
    ShortURL = short.ShortURL,
    MONGO_DB_SHORT = process.env.MONGO_DB_SHORT || 'mongodb://localhost/short';

/*!
  Connect to MongoDB w/ MongooseJS
 */

short.connect(MONGO_DB_SHORT);

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

/*!
  Setup ExpressJS
 */

app.configure('production', function() {
  app.use(express.static(__dirname+'/public'));
  app.use(express.bodyParser());
});

/*!
  API Calls to Generate Short URLs
 */

app.post('/api/*', function(request, response) {
  if (request.url === '/favicon.ico') {
    return;
  }
  var URL = request.body['url'] || req.url.slice(5),
      options = {length: 5};
  short.generate(URL, options, function(error, shortURL) {
    if (error) {
      console.error(error);
    } else {
      var URL = shortURL.URL;
      var hash = shortURL.hash;
      var tiny_url = 'http://' + request.headers.host + '/' + hash;
      console.log('URL is ' + tiny_url);
      response.send({ url:tiny_url });
    }
  });
});

/*!
  Retrieve Short URLs & Redirect
 */

app.get('*', function(request, response) {
  if (request.url === '/favicon.ico') {
    return;
  }
  var hash = request.url.slice(1),
      visitor = req.connection.remoteAddress,
      options = {visitor: visitor};
  short.retrieve(hash, options, function (error, shortURLObject) {
    if (error) {
      console.error(error);
    } else {
      if (shortURLObject) {
        response.redirect(shortURLObject.URL, 302);
      } else {
        response.send('URL not found!', 404);
        response.end();
      }
    }
  });
});

/*!
  ExpressJS, Listen on <port>
 */

app.listen(port, function() {
  console.log('Server running on port ' + port);
});

Connecting (cURL) to the Complete Example

Just add the domain you'd like to the end of localhost:8080/api

$ curl localhost:8080/api/http://www.longdomain.com/

Pull Requests

  • fork
  • clone
  • improve
  • pull request
  • instant karma!

Run Tests (git clone)

$ git clone [email protected]:edwardhotchkiss/short.git
$ cd short
$ npm test

License (MIT)

Copyright (c) 2011, Edward Hotchkiss.

Author: Edward Hotchkiss

Contributors: Kevin Roth

short's People

Contributors

edwardhotchkiss avatar thinkroth avatar

Stargazers

 avatar

Watchers

 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.