Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Promise-based Node.js URL Shortener backed by Mongoose.js

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 examples
Octocat-spinner-32 lib
Octocat-spinner-32 models
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.md
Octocat-spinner-32 package.json
README.md

short

Node.js Tiny URL generator with analytics

Setup

$ npm install short

Basic API Usage

var mongoose = require("mongoose");
var short = require("short")("mongodb://localhost/short");

var URL = "http://nodejs.org/";

short.gen(URL, function(error, shortURL) {
    if (error) {
        console.error(error);
    } else {
        short.get(shortURL.hash, function(error, shortURLObject) {
            if (error) {
                console.error(error);
            } else {
                var URL = shortURLObject[0].URL
                var hash = shortURLObject[0].hash;
                console.log(URL, hash);
            };
        });
    }
});

/* EOF */

As a Server

var http = require("http");
var mongoose = require("mongoose");
var short = require("short")("mongodb://localhost/short");

var app = http.createServer(function(request, response) {
    var hash = request.url.slice(1);
    if (request.url === "/") {
        response.writeHead(200, { "Content-Type" : "text/html" });
        response.write("URL not found!");
        response.end();
    } else {
        short.get(hash, function(error, shortURLObject) {
            if (error) {
                console.error(error);
            } else {
                if (shortURLObject) {
                    var URL = shortURLObject[0].URL;
                    response.writeHead(302, {
                        "Location" : URL
                    });
                    response.end();
                } else {
                    response.writeHead(200, { "Content-Type" : "text/html" });
                    response.write("URL not found!");
                    response.end();
                }
            };
        });
    }
});

app.listen(8080);
console.log("> Open http://localhost:8080/kQ4c");

/* EOF */
Something went wrong with that request. Please try again.