-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (48 loc) · 1.57 KB
/
server.js
File metadata and controls
71 lines (48 loc) · 1.57 KB
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
var express = require('express');
var request = require('request');
var app = express();
app.set('view engine', 'ejs');
app.use(express.static('public'));
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // this is used for parsing the JSON object from POST
var cityList = [
];
app.get('/', function (req, res) {
res.render('meteo', {
list : cityList
});
});
app.post('/sortList', function (req, res) {
var new_index = req.body.new_index;
var old_index = req.body.old_index;
cityListMove(old_index,new_index);
});
function cityListMove(fromIndex, toIndex) {
var element = cityList[fromIndex];
cityList.splice(fromIndex, 1);
cityList.splice(toIndex, 0, element);
}
app.get('/add', function (req, res) {
request("http://api.openweathermap.org/data/2.5/weather?q="+req.query.city+"&APPID=9b754f1f40051783e4f72c176953866e&units=metric&lang=fr", function(error, response, body) {
body = JSON.parse(body);
req.query.city = body.name;
req.query.temp_min = body.main.temp_min;
req.query.temp_max = body.main.temp_max;
req.query.picto = body.weather[0].icon;
req.query.description = body.weather[0].description;
cityList.push(req.query);
res.render('meteo', {
list : cityList
});
});
});
app.get('/delete', function (req, res) {
cityList.splice(req.query.indice, 1);
res.render('meteo', {
list : cityList
});
});
app.listen(process.env.PORT || 80, function () {
console.log("Server listening on port 80");
});