-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (59 loc) · 1.86 KB
/
app.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
// An app to display the weather forecast for the next 4 days
// Jamie Mattocks
var express = require('express');
var app = express();
var request = require('request');
var bodyParser = require('body-parser');
const api_key = '98e621b0-231d-4ddd-bbe8-5a1a1e7ff48b'; // Your api key
app.set('view engine', 'ejs');
app.use(express.static('static'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.json());
app.use(express.urlencoded());
// Landing page
app.get('/', (req, res) => {
res.render('index');
});
app.post('/api', (req, res) => {
console.log(req.body.location_id);
var options = {url: 'http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/json/' +
req.body.location_id +
'?res=daily&' +
'key=' +
api_key,
headers: {
'Content-Type': 'application/json'
}};
request(options, (error, response, body) => {
console.log('error: ', error);
console.log('status code: ', response && response.statusCode);
// console.log('body: ', body);
body = JSON.parse(body);
res.setHeader('Content-Type', 'application/json');
res.send(body);
});
})
// app.post('/test', (req, res) => {
// var options = {url: 'http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/json/' +
// location_id +
// '?res=daily&' +
// 'key=' +
// api_key,
// headers: {
// 'Content-Type': 'application/json'
// }};
// request(options, (error, response, body) => {
// console.log('error: ', error);
// console.log('status code: ', response && response.statusCode);
// // console.log('body: ', body); // Displays the data in the console
// body = JSON.parse(body);
// res.setHeader('Content-Type', 'application/json');
// res.send(body);
// });
// });
// app.get('/test', (req, res) => {
// res.render('index');
// });
app.listen(process.env.PORT, 3000)
console.log('Server ready');