short 开源的短域名服务开源项目

我要开发同款
匿名用户2011年09月28日
182阅读

技术信息

开源地址
https://github.com/edwardhotchkiss/short
授权协议
MIT

作品详情

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

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

$pmistallshort

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

varmogoose=require("mogoose");varshort=require("short");mogoose.coect("mogodb://localhost/short");varURL="https://odejs.org/";short.make(URL,fuctio(error,shortURL){if(error){cosole.error(error);}else{short.get(shortURL.hash,fuctio(error,shortURLObject){if(error){cosole.error(error);}else{varURL=shortURLObject[0].URLvarhash=shortURLObject[0].hash;cosole.log(URL,hash);process.exit(1);};});}});

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

varhttp=require("http");varmogoose=require("mogoose");varshort=require("short");mogoose.coect("mogodb://localhost/short");varapp=http.createServer(fuctio(request,respose){varhash=request.url.slice(1);if(request.url==="/"){respose.writeHead(200,{"Cotet-Type":"text/html"});respose.write("URLotfoud!");respose.ed();}else{short.get(hash,fuctio(error,shortURLObject){if(error){cosole.error(error);}else{if(shortURLObject){varURL=shortURLObject[0].URL;respose.writeHead(302,{"Locatio":URL});respose.ed();}else{respose.writeHead(200,{"Cotet-Type":"text/html"});respose.write("URLotfoud!");respose.ed();}};});}});app.liste(8080);cosole.log(">Opehttps://localhost:8080/kQ4c");

功能介绍

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

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论