DevOps研发效能
媒体矩阵
开源中国APP
授权协议 MIT
操作系统 跨平台
软件类型 开源软件
所属分类 建站系统短网址服务
开源组织
地区 不详
投 递 者 红薯
适用人群 未知
收录时间 2011-09-28

软件简介

short 是一个开源的短域名服务,使用 Node.js 与 MongoDB 搭建,可以直接在你的 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");
展开阅读全文

代码

的 Gitee 指数为
超过 的项目

评论

点击引领话题📣 发布并加入讨论🔥
暂无内容
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
暂无内容
0 评论
25 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部