-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (54 loc) · 1.25 KB
/
index.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
const express = require('express')
const fs = require('fs')
//Functions
const findDate = (date, callback) => {
const calendar = loadCalendar()
const item = calendar.bacs_calendar[date]
if (item) {
callback(undefined, item)
} else {
callback('date_not_found', undefined)
}
}
const loadCalendar = () => {
try {
const dataBuffer = fs.readFileSync('data/2023.json')
const dataJSON = dataBuffer.toString()
return JSON.parse(dataJSON)
} catch (e) {
return []
}
}
//App
const app = express()
const port = process.env.PORT || 3000
//Request Handler
app.get('/', function (req, res) {
if (req.query.date) {
findDate(req.query.date, (error, data) => {
if (error) {
return res.json({
date: req.query.date,
error
})
}
res.json({
date: req.query.date,
data
})
})
} else {
return res.json({
error: "date_not_defined"
})
}
})
app.get('*', (req, res) => {
res.json({
error: 'page_not_found'
})
})
// Start Server
app.listen(port, () => {
console.log('Server is up on port: ' + port)
})