short 开源的短域名服务

程序名称:short

授权协议: 未知

操作系统: 跨平台

开发语言: JavaScript

short 介绍

short是一个开源的短域名服务,使用Node.jsMongoDB搭建,可以直接在你的Server程序中调用,也可以通过Node.js的http server模块以一个服务启动。

可以直接通过Node.js的npm进行安装:

$ npm install short

你可以直接在你的Node.js项目中这样调用,生成长域名对应的短链接:

var mongoose = require("mongoose");
var short = require("short");

mongoose.connect("mongodb://localhost/short");

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

short.make(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);
                process.exit(1);
            };
        });
    }
});

下面代码用于搭建一个提供短域名跳转的HTTP 服务:

var http = require("http");
var mongoose = require("mongoose");
var short = require("short");

mongoose.connect("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");

short 官网

https://github.com/edwardhotchkiss/short

相关编程语言

VertrigoServ 是一个Windows平台下的非常专业的、易...
APMServ 5.2.0 是一款拥有图形界面的快速搭建Apache...
PHP Manager for IIS 是一个用来在 Windows 平台下的...
LAMPPIX 是一个包含 LAMP 运行环境的可启动 Linux 光...
RubyWorks 是一個在Red Hat Enterprise 或是CentOS ...
Instant Rails是一个一站式的Rails运行环境,包含Ru...