Home Web Front-end JS Tutorial nodejs practical example abbreviation restoration_javascript skills

nodejs practical example abbreviation restoration_javascript skills

May 16, 2016 pm 06:13 PM
nodejs

The idea is very simple:
1. httpserver gets the url that needs to be restored;
2. Use httpclient to recursively request this url until http status not in (302, 301) is found.
3. Return to the restored original url.

Okay, the code is as follows:

Copy the code The code is as follows:

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:') { // Unable to request 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 service
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) { // Support jsonp cross-domain request
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);
});

Start you Web server:
$ node urlexpand.js
Open the browser to access directly:
http://127.0.0.1:1235/api?u=http://is.gd/imWyT
Or visit my test server:
http://yongwo.de:1235/api?u=http://is.gd/imWyT&cb=foo
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between nodejs and tomcat The difference between nodejs and tomcat Apr 21, 2024 am 04:16 AM

The difference between nodejs and tomcat

The difference between nodejs and vuejs The difference between nodejs and vuejs Apr 21, 2024 am 04:17 AM

The difference between nodejs and vuejs

Is nodejs a backend framework? Is nodejs a backend framework? Apr 21, 2024 am 05:09 AM

Is nodejs a backend framework?

What is the difference between npm and npm.cmd files in the nodejs installation directory? What is the difference between npm and npm.cmd files in the nodejs installation directory? Apr 21, 2024 am 05:18 AM

What is the difference between npm and npm.cmd files in the nodejs installation directory?

Is nodejs a back-end development language? Is nodejs a back-end development language? Apr 21, 2024 am 05:09 AM

Is nodejs a back-end development language?

Is there a big difference between nodejs and java? Is there a big difference between nodejs and java? Apr 21, 2024 am 06:12 AM

Is there a big difference between nodejs and java?

What are the global variables in nodejs What are the global variables in nodejs Apr 21, 2024 am 04:54 AM

What are the global variables in nodejs

Which one to choose between nodejs and java? Which one to choose between nodejs and java? Apr 21, 2024 am 04:40 AM

Which one to choose between nodejs and java?

See all articles