Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

41 lines (38 sloc) 1.547 kB
"use strict";
var streams = require('streamline/lib/streams/server/streams');
var url = require('url');
var qs = require('querystring');
var begPage = '<html><head><title>My Search</title></head></body>' + //
'<form action="/">Search: ' + //
'<input name="q" value="{q}"/>' + //
'<input type="submit"/>' + //
'</form><hr/>';
var endPage = '<hr/>generated in {ms}ms</body></html>';
streams.createHttpServer(function(request, response, _) {
var query = qs.parse(url.parse(request.url).query),
t0 = new Date();
response.writeHead(200, {
'Content-Type': 'text/html; charset=utf8'
});
response.write(_, begPage.replace('{q}', query.q || ''));
response.write(_, search(_, query.q));
response.write(_, endPage.replace('{ms}', new Date() - t0));
response.end();
}).listen(_, 1337);
console.log('Server running at http://127.0.0.1:1337/');
function search(_, q) {
if (!q || /^\s*$/.test(q)) return "Please enter a text to search";
// pass it to Google
var json = streams.httpRequest({
url: 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' + q,
proxy: process.env.http_proxy
}).end().response(_).checkStatus(200).readAll(_);
// parse JSON response
var parsed = JSON.parse(json);
// Google may refuse our request. Return the message then.
if (!parsed.responseData) return "GOOGLE ERROR: " + parsed.responseDetails;
// format result in HTML
return '<ul>' + parsed.responseData.results.map(function(entry) {
return '<li><a href="' + entry.url + '">' + entry.titleNoFormatting + '</a></li>';
}).join('') + '</ul>';
}
Jump to Line
Something went wrong with that request. Please try again.