-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (48 loc) · 1.42 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
const { getLegislators } = require('./getLegislatorsForPoint')
const koa = require('koa')
const cors = require('@koa/cors')
const bodyParser = require('koa-bodyparser')
const app = new koa()
app.use(cors())
app.use(bodyParser())
function responseError (ctx, code, message) {
ctx.status = code
ctx.type = 'json'
ctx.body = JSON.stringify({ message })
return
}
app.use(async (ctx, next) => {
if (ctx.URL.pathname !== '/list' || ctx.method !== 'POST') {
await next()
return
}
const body = ctx.request.body
if (!Array.isArray(body) || body.some((el) => typeof el.lng !== 'number' || typeof el.lat !== 'number' )) {
responseError(ctx, 400, 'wrong body content')
return
}
ctx.type = 'json'
ctx.body = JSON.stringify(await Promise.all(body.map((el) => getLegislators(el.lng, el.lat).catch((e) => ({ message: e.message })))))
})
app.use(async (ctx, next) => {
if (ctx.URL.pathname !== '/' || ctx.method !== 'GET') {
await next()
return
}
const lng = Number(ctx.query.lng)
const lat = Number(ctx.query.lat)
if (Number.isNaN(lng) || Number.isNaN(lat)) {
responseError(ctx, 400, 'wrong query params')
return
}
ctx.type = 'json'
try {
const result = await getLegislators(lng, lat)
ctx.body = JSON.stringify(result)
} catch (e) {
responseError(ctx, 500, e.message)
}
})
const PORT = 8888
app.listen(PORT)
console.log(`Listening web server on http://0.0.0.0:${PORT}`)