forked from mongo-express/mongo-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
179 lines (141 loc) · 5.49 KB
/
router.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var
express = require('express')
, bodyParser = require('body-parser')
, cookieParser = require('cookie-parser')
, methodOverride = require('method-override')
, errorHandler = require('errorhandler')
, session = require('express-session')
, favicon = require('serve-favicon')
, basicAuth = require('basic-auth-connect')
, logger = require('morgan')
, _ = require('underscore')
, mongodb = require('mongodb')
;
var
routes = require('./routes')
, db = require('./db')
;
var router = function(config) {
var appRouter = express.Router();
var mongo = db(config);
// appRouter configuration
if(config.useBasicAuth){
appRouter.use(basicAuth(config.basicAuth.username, config.basicAuth.password));
}
appRouter.use(favicon(__dirname + '/public/images/favicon.ico'));
appRouter.use(logger('dev'));
appRouter.use('/',express.static(__dirname + '/public'));
appRouter.use(bodyParser.urlencoded({ extended: true }));
appRouter.use(cookieParser(config.site.cookieSecret));
appRouter.use(session({
secret: config.site.sessionSecret,
key: config.site.cookieKeyName,
resave: true,
saveUninitialized: true
}));
appRouter.use(methodOverride(function(req, res) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
var method = req.body._method
delete req.body._method
return method
}
}));
if (process.env.NODE_ENV === 'development') {
appRouter.use(errorHandler());
}
// view helper, sets local variables used in templates
appRouter.all('*', function(req, res, next) {
// ensure a trailing slash on the baseHref (used as a prefix in routes and views)
res.locals.baseHref = req.app.mountpath + (req.app.mountpath[req.app.mountpath.length-1] === '/' ? '' : '/');
res.locals.databases = mongo.databases;
res.locals.collections = mongo.collections;
//Flash messages
if (req.session.success) {
res.locals.messageSuccess = req.session.success;
delete req.session.success;
}
if (req.session.error) {
res.locals.messageError = req.session.error;
delete req.session.error;
}
return next();
});
// route param pre-conditions
appRouter.param('database', function(req, res, next, id) {
//Make sure database exists
if (!_.include(mongo.databases, id)) {
req.session.error = "Database not found!";
return res.redirect(res.locals.baseHref);
}
req.dbName = id;
res.locals.dbName = id;
if (mongo.connections[id] !== undefined) {
req.db = mongo.connections[id];
} else {
mongo.connections[id] = mongo.mainConn.db(id);
req.db = mongo.connections[id];
}
next();
});
// :collection param MUST be preceded by a :database param
appRouter.param('collection', function(req, res, next, id) {
//Make sure collection exists
if (!_.include(mongo.collections[req.dbName], id)) {
req.session.error = "Collection not found!";
return res.redirect(res.locals.baseHref + 'db/' + req.dbName); // XXX
}
req.collectionName = id;
res.locals.collectionName = id;
mongo.connections[req.dbName].collection(id, function(err, coll) {
if (err || coll == null) {
req.session.error = "Collection not found!";
return res.redirect(res.locals.baseHref + 'db/' + req.dbName);
}
req.collection = coll;
next();
});
});
// :document param MUST be preceded by a :collection param
appRouter.param('document', function(req, res, next, id) {
if (id.length == 24) {
//Convert id string to mongodb object ID
try {
id = new mongodb.ObjectID.createFromHexString(id);
} catch (err) {
}
}
req.collection.findOne({_id: id}, function(err, doc) {
if (err || doc == null) {
req.session.error = "Document not found!";
return res.redirect(res.locals.baseHref + 'db/' + req.dbName + '/' + req.collectionName);
}
req.document = doc;
res.locals.document = doc;
next();
});
});
// mongodb mongoMiddleware
var mongoMiddleware = function(req, res, next) {
req.adminDb = mongo.adminDb;
req.databases = mongo.databases; //List of database names
req.collections = mongo.collections; //List of collection names in all databases
//Allow page handlers to request an update for collection list
req.updateCollections = mongo.updateCollections;
next();
};
// routes
appRouter.get('/', mongoMiddleware, routes(config).index);
appRouter.get('/db/:database/export/:collection', mongoMiddleware, routes(config).exportCollection);
appRouter.get('/db/:database/:collection/:document', mongoMiddleware, routes(config).viewDocument);
appRouter.put('/db/:database/:collection/:document', mongoMiddleware, routes(config).updateDocument);
appRouter.delete('/db/:database/:collection/:document', mongoMiddleware, routes(config).deleteDocument);
appRouter.post('/db/:database/:collection', mongoMiddleware, routes(config).addDocument);
appRouter.get('/db/:database/:collection', mongoMiddleware, routes(config).viewCollection);
appRouter.put('/db/:database/:collection', mongoMiddleware, routes(config).renameCollection);
appRouter.delete('/db/:database/:collection', mongoMiddleware, routes(config).deleteCollection);
appRouter.post('/db/:database', mongoMiddleware, routes(config).addCollection);
appRouter.get('/db/:database', mongoMiddleware, routes(config).viewDatabase);
return appRouter;
};
module.exports = router;