Nodejs Practical Example _javascript tips for reducing site reduction

Source: Internet
Author: User
The idea is very simple:
1. Httpserver get the URL that needs to be restored;
2. Use httpclient recursion to request this URL until you find HTTP status not in (302, 301).
3. Returns the original URL of the restore.

Well, the code is as follows:
Copy Code code 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: ') {//cannot 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 your Web server:
$ node Urlexpand.js
Open Browser Direct access:
Http://127.0.0.1:1235/api?u=http://is.gd/imWyT
or access my test server:
Http://yongwo.de:1235/api?u=http://is.gd/imWyT&cb=foo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.