-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
67 lines (60 loc) · 1.69 KB
/
api.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
59
60
61
62
63
64
65
66
67
const config = require('./configs.js')
const express = require('express')
const patio = require('patio')
const bodyParser = require('body-parser')
const hostname = '127.0.0.1'
const port = 3000
var closeResponse = function(response, body)
{
response.statusCode = 200
response.end(body)
patio.disconnect()
}
// API app
const app = express()
app.use(bodyParser.json());
app.use( (request, response, next) =>
{
// Setup headers
response.header('Content-Type', 'application/json')
response.header("Access-Control-Allow-Origin", "*")
response.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
// Connect to database
var DB = patio.connect(config.DB_URL)
next()
})
app.get('/api/games', (request, response) =>
{
var Game = patio.addModel("games")
patio.syncModels().chain( () =>
{
Game.all().chain((games) => {closeResponse(response, JSON.stringify(games))})
})
})
app.put('/api/games', (request, response) =>
{
var Game = patio.addModel("games")
patio.syncModels().chain( () =>
{
Game.save(request.body).chain((games) => {closeResponse(response, JSON.stringify(games))})
})
})
app.delete("/api/games/:gameId", (request, response) =>
{
var Game = patio.addModel("games")
patio.syncModels().chain( () =>
{
Game.removeById(request.params.gameId).chain(closeResponse(response))
})
})
app.post("/api/games/:gameId", (request, response) =>
{
var Game = patio.addModel("games")
patio.syncModels().chain( () =>
{
Game.update(request.body, {id: request.params.gameId}).chain(closeResponse(response))
})
})
app.listen(port, hostname)
console.log(`Running on http://${hostname}:${port}`)