-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestHandlers.js
132 lines (107 loc) · 3.7 KB
/
requestHandlers.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var querystring = require("querystring"),
fs = require("fs"),
formidable = require("formidable");
// request = require("request");
function start(response, postData) {
console.log("Request handler 'start' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/search" '+
'method="post">'+
'<input type="text" name="isbn">'+
'<input type="submit" value="search" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}
function search(response, request) {
console.log("Request handler 'search' was called.");
var form = new formidable.IncomingForm();
isbn='';
form.parse(request, function(error, fields, files) {
console.log("parsing done");
// console.log(fields);
console.log(fields.isbn);
isbn=fields.isbn;
console.log(isbn);
var http = require("http");
console.log('isbn');
console.log(isbn);
var options = {
host: 'www.yakaboo.ua',
port: 80,
path: '/search/?cat=&q='+isbn
};
request = require("request"),
options = {
uri: 'http://www.yakaboo.ua/search/?cat=&q='+isbn,
timeout: 5000,
followAllRedirects: true
};
request( options, function(error, resp, body) {
// console.log( response );
// console.log(response2.request.uri.href);
var cheerio = require('cheerio'),
$ = cheerio.load(body);
title = $('#product-title h1').text();
price = $('.product-essential .price').text();
console.log(title,price);
responseBody = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
title + '<br/>' + price +
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(responseBody);
response.end();
});
});
//978-5-17-075637-7 - зеленая миля. Кинг
}
function isbn(response, request) {
console.log("Request handler 'isbn' was called.");
var url = require('url');
var url_parts = url.parse(request.url, true);
var query = url_parts.query;
console.log(url_parts.query.isbn);
isbn = url_parts.query.isbn;
request = require("request"),
options = {
uri: 'http://www.yakaboo.ua/search/?cat=&q='+isbn,
timeout: 5000,
followAllRedirects: true
};
request( options, function(error, resp, body) {
// console.log( response );
// console.log(response2.request.uri.href);
var cheerio = require('cheerio'),
$ = cheerio.load(body);
title = $('#product-title h1').text();
// price = $('#product-price-1037299 .price span').eq(0).text();
price = "10$";
image = $('.product-image #image').attr('src');
console.log(title,price);
response.writeHead(200, {"Content-Type": "application/json",'charset' : 'utf-8'});
var bookObject = { title: title, cover:image, price: price, "author": "King", "store":"Yakaboo"};
var json = JSON.stringify({
books: bookObject
});
console.log(json);
response.end(json);
});
//978-5-17-075637-7 - зеленая миля. Кинг
}
exports.start = start;
exports.search = search;
exports.isbn = isbn;