-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
58 lines (42 loc) · 1.4 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
var mongo = require('mongodb');
var Server = mongo.Server,
DB = mongo.Db,
BSON = mongo.BSONPure;
var url = require('url');
var urlItems = url.parse(process.env.MONGO_DB);
var server = new Server(urlItems.hostname, urlItems.port, {auto_reconnect: true});
db_init = new DB(urlItems.path.substr(1), server, {safe: true});
var createApp = function(err, db) {
if (err) { throw err; }
var express = require('express'),
buckets = require('./routes/buckets')(db);
uploads = require('./routes/fileUploads')(db);
// mailman = require('./routes/emailer')(db);
var app = express();
var __root = function() {
return require('path').normalize(__dirname);
};
app.configure(function() {
app.use(express.logger('dev'));
app.use(express.bodyParser());
});
app.use(express.favicon( __root() +'/public/favicon.ico'));
app.use(express.static( __root() +'/public'));
app.get("/api", buckets.findAll);
app.post("/api", buckets.addBucket);
app.put("/api/:id", buckets.updateBucket);
app.delete("/api/:id", buckets.deleteBucket);
app.post("/target", uploads.addFile);
app.listen(3000);
console.log('Listening on port 3000...');
};
db_init.open(function(err, db) {
if(urlItems.auth) {
var user = urlItems.auth.split(':');
db_init.authenticate(user[0], user[1], function (err) {
createApp(err, db);
});
} else {
createApp(null, db);
}
});