-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (33 loc) · 1.37 KB
/
index.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
var request = require('request');
var options = {
url: 'https://api.oilpriceapi.com/v1/prices/latest',
headers: {
Authorization: 'Token d9de5f8c8932bf6fedc162aebc7baf6b',
Accept: 'application/json'
}
};
var http = require('http'); // 1 - Import Node.js core module
var server = http.createServer(function (req, res) { // 2 - creating server
request(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
// use data
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<html>');
res.write('<head>');
res.write('<title>Oil Price Display</title>');
res.write('</head>');
res.write('<body>');
res.write('<h1>Oil Price Display</h1>');
res.write('<p>Current price: ' + data['data']['price'] + '</p>');
res.write('<p>Currecy:' + data['data']['currency'] + '</p>');
res.write('<p>Product Name:' + data['data']['code']+ '</p>');
res.write('<p>Type of Pricing: ' + data['data']['type']+ '</p>');
res.write('</body>');
res.write('</html>');
//console.log(data);
}
});
});
server.listen(5000); //3 - listen for any incoming requests
console.log('Node.js web server at port 5000 is running..')