-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
92 lines (76 loc) · 2.04 KB
/
app.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Module dependencies
express = require 'express'
mongoose = require 'mongoose'
path = require 'path'
app = module.exports = express.createServer()
mongoose.connect 'mongodb://localhost/vgarchive'
# Configuration
app.configure ->
app.use express.bodyParser()
app.use express.methodOverride()
app.use app.router
app.use express.static path.join(__dirname, '/public')
app.use express.errorHandler
dumpExceptions: true
showStack: true
# Schema configuration
GameModel = mongoose.model 'Game', new mongoose.Schema
title:
type: String
required: true
system:
type: String
required: true
genre:
type: [String]
required: true
# Routes
app.get '/', (req, res) ->
res.render path.join(__dirname, '/public/index.jade'), {layout: false}
app.get '/api', (req, res) ->
res.send 'VGArchive API is running'
app.get '/api/games', (req, res) ->
GameModel.find (err, games) ->
if not err
return res.send games
else
return console.log err
app.post '/api/games', (req, res) ->
console.log "POST: "
console.log req.body
game = new GameModel
title: req.body.title
system: req.body.system
genre: req.body.genre
game.save (err) ->
if not err
return console.log "created"
else
return console.log err
res.send game
app.get '/api/games/:id', (req, res) ->
GameModel.findById req.params.id, (err, game) ->
if not err
return res.send game
else
return console.log err
app.put '/api/games/:id', (req, res) ->
GameModel.findById req.params.id, (err, game) ->
game.title = req.body.title
game.system = req.body.system
game.genre = req.body.genre
game.save (err) ->
if not err
console.log "updated"
else
console.log err
res.send game
app.delete '/api/games/:id', (req, res) ->
GameModel.findById req.params.id, (err, game) ->
game.remove (err) ->
if not err
console.log "removed"
else
console.log err
app.listen 3000
console.log "Express server listening on port %d", app.address().port