-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.coffee
108 lines (88 loc) · 2.82 KB
/
server.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# require('coffee-script')
express = require 'express'
routes = require './private/routes/routes'
cradle = require 'cradle'
db_access = require './private/db/auth'
config = require './config'
app = module.exports = express()
VIEWS_PATH = __dirname + '/private/views'
PUBLIC_PATH = __dirname + '/public'
PORT = config.config.server.port or process.env.VMC_APP_PORT or 3000
# Configuration
app.configure( ->
app.set 'views', VIEWS_PATH
app.set 'view engine', 'jade'
app.set 'view options', { layout: false } # Option: pretty: true
app.use express.bodyParser()
app.use express.methodOverride()
app.use app.router
app.use express.static( PUBLIC_PATH )
)
app.configure 'development', ->
app.use express.errorHandler { dumpExceptions: true, showStack: true }
app.configure 'production', ->
app.use express.errorHandler()
# Setup the CouchDB connection
conn = new (cradle.Connection)(
cache: true
raw: false
host: "http://127.0.0.1"
port: 5984
auth:
username: db_access.auth.username
password: db_access.auth.password
)
db = conn.database db_access.auth.name
db.exists( (err, exists) ->
if ( err )
throw new Error "an error in DB"
else if ( exists )
console.log "database has exists"
else
db.create()
console.log "create a new database"
)
# Routes
app.get '/', routes.home #routes.home: /routes/routes.js => exports.home()
app.get '/posts/new', routes.newPost #routes.home: /routes/routes.js => exports.home()
# Fetch the blog posts
app.get "/posts", (req, res) ->
db.get "_design/posts/_view/by_title", (err,doc) ->
res.json doc
# Need to be after "/posts", because it is the fallback when 'posts' route is not matched
# when refresh the browser, it will maintain its state
app.get '/:id', routes.home
# Fetch the single blog post
app.get "/post/:id", (req, res) ->
db.get req.params.id, (err, doc) ->
res.json doc
# Create a new blog post
app.post '/posts/new', (req, res ) ->
options =
"title": req.body.title,
"excerpt": req.body.excerpt,
"content": req.body.content
db.save options, (err, response)->
db.get response.id, (err, doc) ->
res.redirect('/') unless err
# Delete the existing post
app.delete '/post/:id', (req, res) ->
db.get req.params.id, (err, doc) ->
db.remove req.params.id, doc._rev, (err, response) ->
res.send response
# Update the Comments in particular post
app.put '/post/:id', (req, res) ->
comment = req.body.comments
db.get req.params.id, (err, doc) ->
unless doc.comments
db.merge req.params.id, { "comments": [comment] }, (err, response) ->
console.log err if err
res.send response
else
doc.comments.push(comment)
db.merge req.params.id, { "comments": doc.comments }, (err, response) ->
console.log err if err
res.send response
app.startServer = ->
app.listen PORT, ->
console.log "Express server listening on port %d in %s mode", PORT, app.settings.env