Skip to content

Instantly share code, notes, and snippets.

@winstonma
Last active November 23, 2020 10:05
Show Gist options
  • Save winstonma/fb303bec4d387f307c7949ccf178ff4f to your computer and use it in GitHub Desktop.
Save winstonma/fb303bec4d387f307c7949ccf178ff4f to your computer and use it in GitHub Desktop.
KMB ETA API using js-kmb-api library
const express = require('express')
const compression = require('compression')
const PORT = process.env.PORT || 5000
const Kmb = require('js-kmb-api').default
let localStorage
const kmb = new Kmb('zh-hant', localStorage)
express()
.use(compression())
.get('/', (req, res) => {
res.send('Hello World!')
})
.get('/getETAsByRoute/:route/:bound/:serviceType', (req, res) => {
kmb.getRoutes(req.params.route)
.then(data => data.find(route => route.bound === parseInt(req.params.bound)))
.then(route => route.getVariants())
.then(data => data.find(route => route.serviceType === parseInt(req.params.serviceType)))
.then(variant => variant.getStoppings())
.then(stoppings => Promise.all(stoppings.map(stopping => stopping.getEtas())))
.then(data => res.send(data))
})
.get('/getETAsByStopID/:stopID', (req, res) => {
const stop = new kmb.Stop(req.params.stopID)
stop.getStoppings()
.then(stoppings => Promise.all(stoppings.map(stopping => stopping.getEtas())))
.then(data => res.send(data))
})
.listen(PORT, () => console.log(`Listening on ${PORT}`))
{
"name": "kmb_api",
"version": "0.1.0",
"description": "A sample KMB API",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"compression": "^1.7.4",
"express": "^4.17.1",
"js-kmb-api": "^3.2.5"
},
"keywords": [
"node",
"heroku",
"express"
],
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment