-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.es6.js
46 lines (39 loc) · 1.18 KB
/
server.es6.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
const express = require('express')
const cors = require('cors')
const upload = require('./lib/upload')
const bodyParser = require('body-parser')
const config = require('./config')
const PORT = 4008
const app = express()
app.enable('trust proxy')
app.disable('x-powered-by')
app.use('*', cors({ origin: '*' }))
app.use('/tiles', express.static(config.STORAGE_PATH))
// check if API key is present
app.use((request, response, next) => {
if (request.headers.authorization) {
const authParts = request.headers.authorization.split(' ')
if (authParts && authParts.length === 2 &&
authParts[0] === 'Bearer' &&
authParts[1] === config.API_KEY) {
next()
return true
}
}
response.status(401).send('API key required')
})
app.use(bodyParser.json({ limit: '250mb' }))
upload(app)
app.use((error, request, response, next) => {
const statusCode = error.status || 500
if (request.accepts('json')) {
response.status(statusCode).send({
error: error.message,
url: request.url
})
} else {
response.status(statusCode).send(error.message)
}
})
// eslint-disable-next-line no-console
app.listen(PORT, () => console.log(`Server running on port ${PORT}`))