-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.js
88 lines (78 loc) · 2.15 KB
/
search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var http = require('http');
var formatter = require('./lib/formatter');
var app = require('express').createServer();
app.register('.html', require('ejs'));
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
function render(req, res, results) {
var skipRender = req.param('_skiprender');
if("true" == skipRender) {
res.writeHead(200);
res.end();
}
else {
res.render('index.ejs', {
searchTerm: req.param('_nkw'),
title: 'My Site',
searchUrl : '/',
results : results,
formatter: formatter,
getVal : function(name) {
return formatter.getValue(results, name);
}
});
}
}
app.get('/', function(req, res) {
if(req.param('_nkw')) {
// TODO: Sanitize
var path = '/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&' +
'SERVICE-VERSION=1.8.0&SECURITY-APPNAME=Foobat96e-1d23-4ae7-8e15-e5874c9cd58&' +
'RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD&' +
'keywords=' + req.param('_nkw') + '&paginationInput.entriesPerPage=50&' +
'outputSelector%280%29=SellerInfo&outputSelector%281%29=CategoryHistogram';
var useProxy = false;
var host = useProxy ? 'localhost' : 'svcs.ebay.com';
var port = useProxy ? 8080 : 80;
var realPath = useProxy ? 'http://svcs.ebay.com' + path : path;
var options = {
host: host,
port: port,
path: realPath
};
http.get(options,
function(clientRes) {
var body = '';
clientRes.on('data', function (chunk) {
body = body + chunk;
});
clientRes.on('end', function(chunk) {
var results = JSON.parse(body);
render(req, res, results);
});
}
).
on('error', function(e) {
console.log("Got error: " + e.message);
});
}
else {
render(req, res, null);
}
});
// Start a cluster
// app.listen(3000);
var Cluster = require('cluster2');
var c = new Cluster({
port: 3000,
cluster: true
});
c.on('died', function(pid) {
console.log('Worker ' + pid + ' died');
});
c.on('forked', function(pid) {
console.log('Worker ' + pid + ' forked');
});
c.listen(function(cb) {
cb(app);
});