-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
80 lines (68 loc) · 2.09 KB
/
app.js
File metadata and controls
80 lines (68 loc) · 2.09 KB
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
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.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.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.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.end(JSON.stringify(collections));
});
});
// retrieve the meta data for all collections
app.get('/api/all', function(req, res) {
mongo.getAllCollections().then(function(collections) {
res.end(JSON.stringify(collections));
});
});
// redirect a direclty requested url to the angular page
// NEEDS TO BE TESTED
// app.get('/:url', function(req, res) {
// res.redirect('/#/' + req.params.url);
// });
// 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);
});