-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.js
33 lines (25 loc) · 858 Bytes
/
program.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
var http = require('http');
var html = '';
var url = 'http://metadata.helmet-kirjasto.fi/search/author.json?query=Campbell';
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type' : 'text/html'});
res.end(html);
}).listen (8181, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8181/');
console.log('Get list of books');
http.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
console.log('Getting books finished');
var bookList = JSON.parse(body).records;
htmlContentList = bookList.map(function(d) {
return '<h1>' + d.title + '</h1>' + '<p>' + d.year + '</p>';
});
html = '<body><html>\n'+htmlContentList.join('\n')+'\n</body></html>';
});
}).on('error', function(e) {
console.log('Error: ', e);
});