-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
126 lines (99 loc) · 3.23 KB
/
server.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
var express = require('express');
var request = require('request');
var mongoose= require('mongoose');
var app = express();
var options = { server: { socketOptions: {keepAlive: 300000, connectTimeoutMS: 30000 } }};
mongoose.connect('mongodb://hg75:[email protected]:13586/meteo', options, function(err) {
});
var citySchema = mongoose.Schema({
name: String,
icon: String,
description:String,
main_temp_max: Number,
main_temp_min: Number,
main_temp: Number,
coord_lon: Number,
coord_lat: Number,
position:Number,
});
var CityModel = mongoose.model('City', citySchema);
app.use(express.static('public'));
app.set('view engine', 'ejs');
var cityList=[];
app.get('/', function(req, res){
CityModel.find().sort({position:1}).exec(function(error, cities3){
cityList = cities3;
res.render('index', {cityList : cityList});
});
});
var indice = -1;
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);
indice += 1;
var cityDb = new CityModel({
name: body.name,
icon: body.weather[0].icon,
description:body.weather[0].description,
main_temp_max: body.main.temp_max,
main_temp_min: body.main.temp_min,
main_temp: body.main.temp,
coord_lon: body.coord.lon,
coord_lat: body.coord.lat,
position:indice
});
cityDb.save(function(error, contact){
CityModel.find().sort({position:1}).exec(function(error, cities3){
cityList = cities3;
console.log(cityList);
res.render('index', {cityList : cityList});
});
// res.render('index', {cityList : cityList});
});
});
});
app.get('/delete', function(req, res){
indice -= 1;
CityModel.remove({position:req.query.position}, function(error){
CityModel.find().sort({position:1}).exec(function(error, cities){
for(var i=0; i<cities.length; i++){
CityModel.update({name:cities[i].name},{position:i}, function(error, raw, list){
});
}
setTimeout(function(){
CityModel.find().sort({position:1}).exec(function(error, cities3){
cityList = cities3;
// console.log(cityList);
res.render('index', {cityList : cityList});
}, 200);
});
});
});
});
app.get('/update', function(req, res){
let position = req.query.place.split(',');
let positionNumber = [];
let ville = req.query.ville.split(',');
let villeParse = [];
for(var i =0;i<position.length;i++){
positionNumber.push(parseInt(position[i]));
villeParse.push(ville[i]);
}
CityModel.find(function(error, cities){
for(var j = 0; j<cities.length; j++){
CityModel.update({name:villeParse[j]}, {position:villeParse.indexOf(villeParse[j])}, function(error, raw){
});
}
});
setTimeout(function(){
CityModel.find().sort({position:1}).exec(function(error, cities3){
cityList = cities3;
console.log(cityList);
// res.render('index', {cityList : cityList});
});
}, 500);
});
const port = (process.env.PORT || 8080);
app.listen(port, () => {
console.log('ok ecoute sur port 8080');
});