forked from CompSciClub/Hopkins-Planner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
107 lines (83 loc) · 3.08 KB
/
app.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
(function(){
"use strict";
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes/index.old.js')
, routeList = require("./routes/index.js")
, mongoose = require("mongoose")
, MongoStore = require("connect-mongodb")
, auth = require("./app/auth")
, _ = require("underscore")
, schemas = require("./schemas");
var URL = process.env.URL || "localhost:3000"; // UGH globals
var app = module.exports = express.createServer(), errorHandler;
var mongoURI = process.env.MONGOLAB_URI || "mongodb://127.0.0.1/planner";
mongoose.connect(mongoURI);
mongoose.connection.on("open", function(){
app.configure(function(){
app.set('views', __dirname + '/templates');
app.set('view engine', 'jade');
app.set("view options", {"layout": false});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session( {store: new MongoStore({db: mongoose.connection.db}), cookie: {httpOnly: true, maxAge: 604800000}, secret: process.env.secret || "The computer science club will rise again... and reclaim B204" }));
app.use(express["static"](__dirname + '/public'));
app.use(app.router);
app.use(errorHandler);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
_.each(routeList.list, function(route){
var methods = route[4] || ["get"];
methods.forEach(function(method){
var params = [], confVar = -1;
// TODO: auth and schemas
/*if (route[2] === true){
confVar = 1;
}
params.push(auth(conf, confVar));*/
// add in ordrinApi
if (route[3]){
params.push(auth(null, route[3]));
}
app[method](route[0], params, route[1]);
});
});
app.get("/monthly", routes.monthly);
app.get("/verify/:token", routes.verify);
// events
app.post("/createClass", routes.createClass);
app.post("/addStudent", routes.addStudent);
// administrative tasks
app.post("/holiday", routes.createHoliday);
app.get("/holiday", routes.createHoliday_page);
app.get("*", routes.noPage);
console.log("starting server on port", process.env.PORT);
app.listen(process.env.PORT || 3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
process.on("uncaughtException", function(err){
console.trace(err);
});
errorHandler = function(err, req, res, next){
var routes = {
401: "401.jade",
500: "errors/500.jade",
404: "404.jade"
};
if (_.has(routes, err)){
var params = {title: "Error: " + err, loggedIn: req.session.valid, name: req.session.displayName};
res.render(routes[err], params);
} else {
next(err);
}
};
}());