您好,欢迎访问一九零五行业门户网

nodejs实用示例 缩址还原_javascript技巧

思路非常简单:
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
其它类似信息

推荐信息