-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (51 loc) · 1.73 KB
/
server.js
File metadata and controls
70 lines (51 loc) · 1.73 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
const express = require('express')
const app = express()
const config = require('./config')
const Station = require('./models/stations')
const axios = require('axios')
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true }))
app.get('/', (req, res) => {
res.render('index', {
googleApiKey: config.googleApiKey,
})
})
app.get('/api/station/all', (req, res) => {
Station.findAll().then((dbRes) => res.json(dbRes))
})
app.get('/api/owners', (req, res) => {
Station.total_stations().then((owners) => res.json(owners))
})
app.get('/api/stats', (req, res) => {
Station.stats().then((stats) => res.json(stats))
})
app.get('/api/station/random', (req, res) => {
Station.random_station().then((random) => res.json(random))
})
app.get('/api/station/bounds', (req, res) => {
const { south, north, west, east } = req.query
Station.in_bounds(south, north, west, east).then((stations) =>
res.json(stations)
)
})
app.get('/api/stations/nearest', (req, res) => {
const latitude = parseFloat(req.query.latitude)
const longitude = parseFloat(req.query.longitude)
const radius = parseFloat(req.query.radius)
const limit = 700
Station.nearest_station( latitude , longitude , radius , limit )
.then((results) => res.json(results))
})
app.get('/api/commodities', async (req, res) => {
try {
const response = await axios.get(`https://commodities-api.com/api/latest?access_key=${process.env.COMMODITIES_API_KEY}&base=USD&symbols=WTIOIL,BRENTOIL,NG`)
res.json(response.data)
} catch (error) {
console.error(error)
res.status(500).json({ error: 'An error occurred while fetching commodity data' })
}
})
app.listen(config.port, () => {
console.log(`listening on port ${config.port}`)
})