-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
120 lines (98 loc) · 3.35 KB
/
server.js
File metadata and controls
120 lines (98 loc) · 3.35 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
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
var express = require('express');
var fetch = require('node-fetch');
var app = express();
const PORT = process.env.PORT || 5000
app.get('/', (request, response) => {
response.send({
port: PORT,
github: 'https://github.com/renabil/api',
api: {
'Reddit': {
endpoint: '/api/reddit/:subreddit/:limit?',
params: {
subreddit: 'Preferred subreddit',
limit: 'max 100, default 25'
}
},
'Bus Stops (By Code)': {
endpoint: '/api/bus/stop/code/:busstopnumber?',
params: {
busstopnumber: 'optional, returns all bus stops if the param is not inside the link'
}
},
'Bus Stops (By Name)': {
endpoint: '/api/bus/stop/name/:busstopname?',
params: {
busstopname: 'optional, returns all bus stops if the param is not inside the link'
}
}
}
})
})
// REDDIT
app.get('/api/reddit/:subreddit/:limit?', (request, response) => {
var subreddit = request.params.subreddit;
var limit = request.params.limit;
if (limit) {
fetch('https://www.reddit.com/r/' + subreddit + '/new.json?limit=' + limit)
.then(response => response.json())
.then(json => {
response.send(json)
})
} else {
fetch('https://www.reddit.com/r/' + subreddit + '/new.json')
.then(response => response.json())
.then(json => {
response.send(json)
})
}
})
// BUS STOPS (CODE)
app.get('/api/bus/stop/code/:busstopnumber?', (request, response) => {
var busstopnumber = request.params.busstopnumber;
if (busstopnumber) {
fetch('https://busrouter.sg/data/2/bus-stops.json')
.then(response => response.json())
.then(json => {
var rtnvalue = json.filter(data => {
return data.no.match(busstopnumber)
})
response.send(rtnvalue)
})
} else {
fetch('https://busrouter.sg/data/2/bus-stops.json')
.then(response => response.json())
.then(json => {
var rtnvalue = json.filter(data => {
return data.no.match()
})
response.send(rtnvalue)
})
}
})
// BUS STOPS (NAME)
app.get('/api/bus/stop/name/:busstopname?', (request, response) => {
var busstopname = request.params.busstopname.toLowerCase();
if (busstopname) {
fetch('https://busrouter.sg/data/2/bus-stops.json')
.then(response => response.json())
.then(json => {
var rtnvalue = json.filter(data => {
return data.name.toLowerCase().match(busstopname)
})
response.send(rtnvalue)
})
} else {
fetch('https://busrouter.sg/data/2/bus-stops.json')
.then(response => response.json())
.then(json => {
var rtnvalue = json.filter(data => {
return data.name.toLowerCase().match()
})
response.send(rtnvalue)
})
}
})
app.listen(PORT, () => {
console.log('Server is running!')
})