-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
46 lines (40 loc) · 1.67 KB
/
server.js
File metadata and controls
46 lines (40 loc) · 1.67 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
const weather = require('./backend/weather');
const favorites = require('./backend/favorites');
const express = require('express');
const app = express();
const path = require('path');
const weatherRouter = express.Router();
weatherRouter.get('/city', function (req, res) {
const q = req.query.q;
console.log(q);
weather.load(q).then(result => {
result.json().then(json => res.send(json));
}).catch(err => res.sendStatus(404))
});
const favoritesRouter = express.Router();
favoritesRouter.get('', function (req, res) {
favorites.getAll().then(result => { res.json(result); console.log(result); }).catch(reject => res.sendStatus(404));
})
.post('', function (req, res) {
const city = req.query.city;
weather.load(city).then(async result => {
resultJson = await result.json()
if (result.ok && resultJson.location.name) {
cityName = resultJson.location.name
favorites.add(cityName).then(result => res.json({name: result})).catch(reject => {
console.log("Status " + reject.status);
res.sendStatus(reject.status)
});
} else {
console.log("Not found " + city)
res.sendStatus(404);
}
}).catch(err => {console.log(err); res.sendStatus(500)})
}).delete('', function (req, res) {
const city = req.query.city;
favorites.deleteCity(city).then(result => res.sendStatus(200)).catch(reject => res.sendStatus(404));
});
app.use('/weather', weatherRouter);
app.use('/favorites', favoritesRouter);
app.use(express.static(path.join(__dirname + '/frontend')));
app.listen(3000);