-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (61 loc) · 1.82 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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var mongo_url = (process.env.CARTERO_MONGO_URL || 'localhost')
mongoose.connect('mongodb://' + mongo_url + '/cartero');
var Notification = require('./app/models/notification');
app.use(bodyParser.json());
var router = express.Router();
router.use(function(req, res, next) {
console.log('Something is happening.');
next();
});
app.use('/', router);
router.route('/notifications')
.get(function(req, res) {
Notification.find({
user_id: req.header("x-consumer-custom-id")
}, function(err, notifications) {
if (err)
res.send(err);
res.json({
"notifications": notifications.map(function(notification){
return {
"notification_id": notification._id,
"title": notification.title,
"body": notification.body,
"link": notification.link,
"image": notification.image
}
})
});
});
});
router.route('/notifications/:notification_id')
.delete(function(req, res) {
Notification.remove({
_id: req.params.notification_id
}, function(err, notification) {
if (err)
res.send(err);
res.status(204).end();
});
});
router.route('/internal/notifications')
.post(function(req, res) {
var notification = new Notification();
notification.user_id = req.body.user_id;
notification.title = req.body.title;
notification.body = req.body.body;
notification.link = req.body.link;
notification.image = req.body.image;
notification.save(function(err) {
if (err)
res.send(err);
res.status(204).end();
});
})
var port = process.env.CARTERO_PORT || 3000;
app.listen(port);
console.log('listen on port ' + port);