npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

short

v2.6.0

Published

Node.js URL Shortener backed by Mongoose.js

Downloads

269

Readme

short Build Status Dependency Status Git Tip

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.

Author: Edward Hotchkiss

Bitdeli Badge