-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.js
35 lines (29 loc) · 974 Bytes
/
routes.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
module.exports = (models, socket) => {
const router = require('express').Router();
router.get('/', (req, res) => {
res.send('index.html');
});
router.get('/api/:collection', (req, res) => {
const model = models[req.params.collection];
model.find((err, results) => {
res.send(results);
});
});
router.get('/api/:collection/:id/:subcollection', (req, res) => {
const model = models[req.params.subcollection];
model.find({ thread: req.params.id }, (err, results) => {
res.send(results);
});
});
router.post('/api/:collection', (req, res) => {
const model = models[req.params.collection];
model.create(req.body, (err, data) => {
if (!err) socket.emit(
`new-${model.modelName.toLowerCase()}-${req.body.thread}`,
data
);
});
res.send(req.body);
});
return router
};