forked from elasticnotorious/curates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
101 lines (87 loc) · 2.87 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
97
98
99
100
101
var mongo = require('./mongo');
var express = require('express');
var session = require('express-session');
var url = require('url');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var port = process.env.PORT || 3000;
app = express();
//middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser());
app.use(session({
secret: "bob the builder"
}));
//serve static files in client when referred to in html
app.use(express.static(__dirname + '/client'));
// add new collection endpoint
// responds with null if collection can't be added
app.post('/api/collection/create', function(req, res) {
mongo.create(req.body).then(function(collection) {
res.statusCode = 201;
res.header('Content-Type', "application/json");
res.end(JSON.stringify(collection));
});
});
// update collection endpoint
// responds with the updated collection
app.post('/api/collection/update', function(req, res) {
mongo.update(req.body).then(function(collection) {
res.header('Content-Type', "application/json");
res.end(JSON.stringify(collection));
});
});
// add a link to collection
app.post('/api/collection/addlink', function(req, res) {
mongo.addLink(req.body).then(function(collection) {
res.header('Content-Type', "application/json");
res.end(JSON.stringify(collection));
});
});
// add a star to a collection
// this will check if the given user has already stared the collection
// and if not, add the user to userStars and increment stars.
// responds with the collection, updated or not
app.post('/api/collection/addStar', function(req, res) {
mongo.addStar(req.body).then(function(collection) {
res.header('Content-Type', "application/json");
res.end(JSON.stringify(collection));
});
});
// retrieve a collection by url
app.get('/api/collection/:url', function(req, res) {
mongo.findByUrl(req.params.url).then(function(collection) {
res.header('Content-Type', "application/json");
res.end(JSON.stringify(collection));
});
});
// retrieve the meta data for all of a users collections
app.get('/api/user/:userProvider/:userId', function(req, res) {
var user = {
provider: req.params.userProvider,
id: req.params.userId
};
mongo.getUserCollections(user).then(function(collections) {
res.header('Content-Type', "application/json");
res.end(JSON.stringify(collections));
});
});
// retrieve the meta data for all collections
app.get('/api/all', function(req, res) {
mongo.getAllCollections().then(function(collections) {
res.header('Content-Type', "application/json");
res.end(JSON.stringify(collections));
});
});
// route all other requests to the main page
app.use(function(req, res) {
res.sendFile(__dirname + '/client/index.html');
});
// start the server
app.listen(port, function() {
console.log('listening on', port);
});
module.exports = app;