-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (29 loc) · 845 Bytes
/
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
'use strict'
const path = require('path')
const express = require('express')
const corser = require('corser')
const compression = require('compression')
const serveStatic = require('serve-static')
const fetchScooters = require('./lib/fetch-scooters')
const clientDir = path.join(__dirname, 'client')
const api = express()
api.use(compression())
// CORS proxy
api.get('/scooters', corser.create(), (req, res, next) => {
fetchScooters()
.then(data => res.json(data))
.catch(next)
})
api.use(serveStatic(clientDir))
api.use((err, req, res, next) => {
if (process.env.NODE_ENV === 'dev') console.error(err)
if (res.headersSent) return next()
res.status(err.statusCode || 500).json({error: true, msg: err.message})
next()
})
api.listen(process.env.PORT || 3000, (err) => {
if (err) {
console.error(err)
process.exitCode = 1
}
})