-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (112 loc) · 4.07 KB
/
index.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var http = require('http'),
express = require('express'),
bodyParser = require('body-parser');
path = require('path'),
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
CollectionDriver = require('./collectionDriver').CollectionDriver,
FileDriver = require('./fileDriver').FileDriver; //<---
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
//app.use(express.bodyParser()); // <-- add
app.use(bodyParser.json());
var mongoHost = 'localHost';
var mongoPort = 27017;
var fileDriver; //<--
var collectionDriver;
var mongoClient = new MongoClient(new Server(mongoHost, mongoPort));
mongoClient.open(function(err, mongoClient) {
if (!mongoClient) {
console.error("Error! Exiting... Must start MongoDB first");
process.exit(1);
}
var db = mongoClient.db("MyDatabase");
fileDriver = new FileDriver(db); //<--
collectionDriver = new CollectionDriver(db);
});
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function (req, res) {
res.send('<html><body><h1>Hello World</h1></body></html>');
});
app.post('/files', function(req,res) {fileDriver.handleUploadRequest(req,res);});
app.get('/files/:id', function(req, res) {fileDriver.handleGet(req,res);});
app.get('/:collection', function(req, res, next) {
var params = req.params;
var query = req.query.query; //1
if (query) {
query = JSON.parse(query); //2
collectionDriver.query(req.params.collection, query, returnCollectionResults(req,res)); //3
} else {
collectionDriver.findAll(req.params.collection, returnCollectionResults(req,res)); //4
}
});
function returnCollectionResults(req, res) {
return function(error, objs) { //5
if (error) { res.send(400, error); }
else {
if (req.accepts('html')) { //6
res.render('data',{objects: objs, collection: req.params.collection});
} else {
res.set('Content-Type','application/json');
res.send(200, objs);
}
}
}
}
app.get('/:collection/:entity', function(req, res) { //I
var params = req.params;
var entity = params.entity;
var collection = params.collection;
if (entity) {
collectionDriver.get(collection, entity, function(error, objs) { //J
if (error) { res.send(400, error); }
else { res.send(200, objs); } //K
});
} else {
res.send(400, {error: 'bad url', url: req.url});
}
});
app.post('/:collection', function(req, res) { //A
var object = req.body;
var collection = req.params.collection;
collectionDriver.save(collection, object, function(err,docs) {
if (err) { res.status(400).send(err); }
else { res.send(201, docs); } //B
});
});
app.put('/:collection/:entity', function(req, res) { //A
var params = req.params;
var entity = params.entity;
var collection = params.collection;
if (entity) {
collectionDriver.update(collection, req.body, entity, function(error, objs) { //B
if (error) { res.status(400).send(error); }
else { res.status(200).send(objs); } //C
});
} else {
var error = { "message" : "Cannot PUT a whole collection" }
res.status(400).send(error);
}
});
app.delete('/:collection/:entity', function(req, res) { //A
var params = req.params;
var entity = params.entity;
var collection = params.collection;
if (entity) {
collectionDriver.delete(collection, entity, function(error, objs) { //B
if (error) { res.send(400, error); }
else { res.send(200, objs); } //C 200 b/c includes the original doc
});
} else {
var error = { "message" : "Cannot DELETE a whole collection" }
res.send(400, error);
}
});
app.use(function (req,res) {
res.render('404', {url:req.url});
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});