-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.js
96 lines (84 loc) · 2.31 KB
/
server.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
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
const express = require('express')
const r = require('rethinkdbdash')()
const bodyParser = require('body-parser')
const webpack = require('webpack')
const config = require('./build/webpack.dev.conf')
const _ = require('lodash')
const app = express()
const router = express.Router()
const compiler = webpack(config)
const jsonParser = bodyParser.json()
// handle fallback for HTML5 history API
app.use(require('connect-history-api-fallback')())
// serve webpack bundle output
app.use(require('webpack-dev-middleware')(compiler, {
publicPath: config.output.publicPath,
stats: {
colors: true,
chunks: false
}
}))
// enable hot-reload and state-preserving
// compilation error display
app.use(require('webpack-hot-middleware')(compiler))
router.get('/projects', (req, res) => {
r.table("projects").orderBy('createdAt').run().then(result => {
res.send(result)
}).catch(err => {
console.log("Error:", err)
})
})
router.post('/projects', jsonParser, (req, res) => {
const project = {
'title': req.body.title,
'content': req.body.content,
'createdAt': new Date().toJSON()
}
r.table('projects').insert(project).run().then(result => {
res.send(result)
}).catch(err => {
console.log('Error:', err)
})
})
router.get('/projects/:id', (req, res) => {
r.table('projects').get(req.params.id).run().then(result => {
res.send(result)
}).catch(err => {
console.log('Error:', err)
})
})
router.get('/projects/:id/feeds', (req, res) => {
r.table('feeds').filter({projectId: req.params.id}).orderBy('createdAt').run().then(result => {
res.send(result)
}).catch(err => {
console.log('Error:', err)
})
})
router.get('/feeds', (req, res) => {
r.table("feeds").orderBy('createdAt').run().then(result => {
res.send(result)
}).catch(err => {
console.log("Error:", err)
res.send(result)
})
})
router.post('/feeds', jsonParser, (req, res) => {
const feed = {
'content': req.body.content,
'createdAt': new Date().toJSON(),
'projectId': req.body.projectId
}
r.table('feeds').insert(feed).run().then(result => {
res.send(result)
}).catch(err => {
console.log('Error:', err)
})
})
app.use('/api', router)
app.listen(8090, 'localhost', function (err) {
if (err) {
console.log(err)
return
}
console.log('Listening at http://localhost:8090')
})