-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
46 lines (40 loc) · 1.01 KB
/
index.coffee
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
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
dbConfig = require('./knexfile')
express = require('express')
knex = require('knex')(dbConfig[process.env.NODE_ENV])
bookshelf = require('bookshelf')(knex)
bodyParser = require('body-parser')
app = express()
Score = bookshelf.Model.extend({
tableName: 'scores'
hasTimestamps: true
})
corsMiddleware = (req, res, next) ->
res.header('Access-Control-Allow-Origin', '*');
next()
app.use(corsMiddleware)
app.use(bodyParser.json())
app.route('/scores')
.all( (req, res, next) ->
res.set('Content-Type', 'application/json')
next()
)
.get((req, res, next) ->
Score.collection().query( (qb) ->
qb.orderBy('score', 'desc').limit(20)
).fetch().then( (collection) ->
res.json(collection)
, (error) ->
res.status(500)
res.end()
)
)
.post( (req, res, next) ->
Score.forge(req.body).save().then( (score) ->
res.json(score)
, (error) ->
res.status(400)
res.end()
)
)
app.listen(3000)