-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
82 lines (71 loc) · 2.45 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
require('dotenv').config()
const OpenWeather = require('./src/services/OpenWeather')
const weatherResponseOneDay = require('./function/apiOneDay')
const weatherResponseSevenDay = require('./function/api7Days')
const weatherResponsechartsDay = require('./function/charts')
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const weather = require('./src/controllers/weather')
const router = express()
router.use(bodyParser.urlencoded({extended: false}))
router.use(bodyParser.json())
router.use(cors())
// le moteur de modèle à utiliser ici ejs
router.set('view engine', 'ejs')
// Pour utiliser plusieurs répertoires statiques actifs
router.use(express.static('public'))
router.get('/', async function (req, res) {
const { lat, lon } = req.query
if (lat === undefined || lon === undefined) {
res.render('index', {
weather: null,
error: null
})
return undefined
}
const weatherService = new OpenWeather()
const weatherResponse = await weatherService.today(lat, lon)
res.render('index', weatherResponseOneDay.transformWeatherResponse(weatherResponse))
})
router.use('/api/weather', weather)
// Route 7 days
router.get('/sevendays', async function (req, res) {
const { lat, lon } = req.query
if (lat === undefined || lon === undefined) {
res.render('api', {
weather: null,
error: null
})
return undefined
}
const weatherService = new OpenWeather()
const weatherResponse = await weatherService.sevendays(lat, lon)
res.render('api', weatherResponseSevenDay.response7days(weatherResponse))
})
router.use('/api/sevendays', weather)
// route charts 7 days
router.get('/chartdays', async function (req, res) {
const { lat, lon } = req.query
if (lat === undefined || lon === undefined) {
res.render('charts', {
weather: null,
error: null
})
return undefined
}
const weatherService = new OpenWeather()
const weatherResponse = await weatherService.chartdays(lat, lon)
// console.log(weatherResponse)
res.render('charts', weatherResponsechartsDay.responsecharts(weatherResponse))
})
router.use('/api/chartdays', weather)
// route about
router.get('/about', function(req, res) {
res.render('about.ejs')
})
const ENV = process.env.HOST_PORT
const hostPort = ENV || 3000
router.listen(hostPort, () => {
console.log(`listening http://localhost:${hostPort}`)
})