nodejs实用示例 缩址还原


Posted in NodeJs onDecember 28, 2010

思路非常简单:
1. httpserver获取需要还原的url;
2. 使用httpclient递归请求这个url,直到发现http status not in (302, 301)为止。
3. 返回还原的原url。

好吧,代码如下:

var net = require('net'), 
http = require('http'), 
url = require('url'), 
fs = require('fs'); 
var DEFAULT_PORTS = { 
'http:': 80, 
'https:': 443 
}; 
var INDEX_TPL = fs.readFileSync('index.html'); 
function _write(str, res, content_type) { 
if(res.jsonp_cb) { 
str = res.jsonp_cb + '("' + str + '")'; 
} 
res.writeHead(200, { 
'Content-Length': str.length, 
'Content-Type': content_type || 'text/plain' 
}); 
res.end(str); 
}; 
function expand(short_url, res) { 
var info = url.parse(short_url); 
// console.log('info: ' + JSON.stringify(info)); 
if(info.protocol != 'http:') { // 无法请求https的url? 
_write(short_url, res); 
return; 
} 
var client = http.createClient(info.port || DEFAULT_PORTS[info.protocol], info.hostname); 
var path = info.pathname || '/'; 
if(info.search) { 
path += info.search; 
} 
var headers = { 
host: info.hostname, 
'User-Agent': 'NodejsSpider/1.0' 
}; 
var request = client.request('GET', path, headers); 
request.end(); 
request.on('response', function (response) { 
if(response.statusCode == 302 || response.statusCode == 301) { 
expand(response.headers.location, res); 
} else { 
_write(short_url, res); 
} 
}); 
}; 
//expand('http://sinaurl.cn/hbMUII'); 
// http服务 
http.createServer(function(req, res){ 
if(req.url.indexOf('/api?') == 0) { 
var params = url.parse(req.url, true); 
if(params.query && params.query.u) { 
if(params.query.cb) { // 支持jsonp跨域请求 
res.jsonp_cb = params.query.cb; 
} 
expand(params.query.u, res); 
} else { 
_write('', res); 
} 
} else { 
_write(INDEX_TPL, res, 'text/html'); 
} 
}).listen(1235); 
process.on('uncaughtException', function (err) { 
console.log('Caught exception: ' + err); 
});

启动你的web服务器吧:
$ node urlexpand.js
打开浏览器直接访问:
http://127.0.0.1:1235/api?u=http://is.gd/imWyT
或者访问我的测试服务器:
http://yongwo.de:1235/api?u=http://is.gd/imWyT&cb=foo
NodeJs 相关文章推荐
nodejs实现的一个简单聊天室功能分享
Dec 06 NodeJs
Nodejs学习笔记之Global Objects全局对象
Jan 13 NodeJs
Nodejs学习笔记之NET模块
Jan 13 NodeJs
Nodejs实现批量下载妹纸图
May 28 NodeJs
Nodejs如何复制文件
Mar 09 NodeJs
用nodejs的实现原理和搭建服务器(动态)
Aug 10 NodeJs
nodeJS删除文件方法示例
Dec 25 NodeJs
Nodejs读取文件时相对路径的正确写法(使用fs模块)
Apr 27 NodeJs
Nodejs+express+ejs简单使用实例代码
Sep 18 NodeJs
详解IWinter 一个路由转控制器的 Nodejs 库
Nov 15 NodeJs
nodejs+mongodb+vue前后台配置ueditor的示例代码
Jan 02 NodeJs
NodeJs 文件系统操作模块fs使用方法详解
Nov 26 NodeJs
golang、python、php、c++、c、java、Nodejs性能对比
Mar 12 #NodeJs
PHPStorm 2020.1 调试 Nodejs的多种方法详解
Sep 17 #NodeJs
nodejs 后缀名判断限制代码
Mar 31 #NodeJs
NodeJS框架Express的模板视图机制分析
Jul 19 #NodeJs
用nodejs访问ActiveX对象,以操作Access数据库为例。
Dec 15 #NodeJs
NodeJS 模块开发及发布详解分享
Mar 07 #NodeJs
nodejs入门详解(多篇文章结合)
Mar 07 #NodeJs
You might like
php mysql索引问题
2008/06/07 PHP
应用开发中涉及到的css和php笔记分享
2011/08/02 PHP
PHP树形结构tree类用法示例
2019/02/01 PHP
php web环境和命令行环境下查找php.ini的位置
2019/07/17 PHP
使用非html5实现js板连连看游戏示例代码
2013/09/22 Javascript
jQuery使用之设置元素样式用法实例
2015/01/19 Javascript
分享js粘帖屏幕截图到web页面插件screenshot-paste
2020/08/21 Javascript
ECMAScript6 新特性范例大全
2017/03/24 Javascript
JavaScript之Date_动力节点Java学院整理
2017/06/28 Javascript
Webstorm2016使用技巧(SVN插件使用)
2018/10/29 Javascript
vue通过style或者class改变样式的实例代码
2018/10/30 Javascript
Javascript ParentNode和ChildNode接口原理解析
2020/03/16 Javascript
vue中全局路由守卫中替代this操作(this.$store/this.$vux)
2020/07/24 Javascript
详解JavaScript自定义函数
2020/07/29 Javascript
js 函数性能比较方法
2020/08/24 Javascript
[01:11:48]Fnatic vs IG 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
python字符串连接的N种方式总结
2014/09/17 Python
python实现批量图片格式转换
2020/06/16 Python
Python3爬虫学习之MySQL数据库存储爬取的信息详解
2018/12/12 Python
详解pyppeteer(python版puppeteer)基本使用
2019/06/12 Python
十行代码使用Python写一个USB病毒
2019/06/21 Python
python实现图片九宫格分割
2021/03/07 Python
Python使用字典实现的简单记事本功能示例
2019/08/15 Python
Python datetime包函数简单介绍
2019/08/28 Python
Python zip函数打包元素实例解析
2019/12/11 Python
pygame实现弹球游戏
2020/04/14 Python
css3 background属性调整增强介绍
2010/12/18 HTML / CSS
Mountain Warehouse德国官网:英国户外零售商
2019/08/11 全球购物
信息系统专业个人求职信范文
2013/12/07 职场文书
工厂保安员岗位职责
2014/01/31 职场文书
小学生读书感言
2014/02/12 职场文书
激励员工的口号
2014/06/16 职场文书
公司演讲稿开场白
2014/08/25 职场文书
党支部先进事迹材料
2014/12/24 职场文书
长城导游词400字
2015/01/30 职场文书
angular异步验证器防抖实例详解
2022/03/31 Javascript