-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
45 lines (36 loc) · 1.11 KB
/
server.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
var http = require('http')
var corsify = require('corsify')
var createApp = require('appa')
var boundaries = require('seattle-boundaries')
var data = require('seattle-boundaries/data')
var topojson = require('topojson')
var config = require('./config')
var app = createApp()
var host = process.env.NODE_ENV === 'development' ?
config.url + ':' + config.port :
config.url
var cors = corsify({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET'
})
http.createServer(cors(app)).listen(config.port)
app.on('/', function (req, res, ctx) {
app.send(res, { boundaries: host + '/boundaries' })
})
app.on('/boundaries', function (req, res, ctx) {
var query = ctx.query
var format = query.format || 'geojson'
if (!query.long || !query.lat) {
var datasets = query.full ? data : Object.keys(data)
return app.send(res, { datasets: datasets })
}
var matches = boundaries({
long: query.long || query.lng || query.lon,
lat: query.lat,
dataset: query.dataset
})
if (format === 'topojson') {
matches = topojson.topology({ collection: matches })
}
app.send(res, matches)
})