-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
218 lines (184 loc) · 5.6 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Module dependencies.
*/
var express = require('express');
var Mongoose = require('mongoose');
var db = Mongoose.connect('mongodb://localhost/db');
var app = module.exports = express.createServer();
var connect = require('connect');
var MemStore = connect.session.MemoryStore;
var passwordHash = require('password-hash');
// My Controllers/Modules
var regController = require('./myModules/register');
var pageController = require('./myModules/page');
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
app.use(express.static(__dirname + '/public'));
app.use(express.cookieParser());
app.use(express.session({
secret: "paris"
/*store:MemStore( {
reapInterval: 6000*10 // interval at which stale sessions are checked
})*/
}));
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Helpers
app.dynamicHelpers({
session: function(req,res){
return req.session;
},
flash: function(req,res){
return req.flash();
},
redir: function(req, res){
if(req.body && req.body.redir){
return req.body.redir;
}else{
return null;
}
}});
// Models
require('./models/users');
var User = db.model('User');
// Session middleware
function requiresLogin(req, res, next){
if(!req.session || !req.session.user){
res.redirect('/sessions/new?redir='+req.url);
} else {
next();
}
};
// Routes
// Login
app.get('/sessions/new', function(req, res){
res.render('login',{
title:'Login',
locals:{
redir: req.query.redir
}
});
});
app.post('/sessions', function(req, res){
User.authenticate(req.body.username, req.body.password, function(user){
if(user){
req.session.user = user;
res.redirect(req.body.redir || '/'+req.body.username+'/index');
} else{
req.flash('warn', 'Login failed');
res.render('login', {
title: 'Login',
locals: {redir:req.body.redir}
});
}
});
});
// Create a new user
app.post('/register', function(req, res){
console.log('register route hit');
regController.handle(req, User, function(type, msg){
if(type==='warn'){
req.flash(type, msg);
res.render('login',{
title:'Login',
locals:{
redir: req.query.redir
}
});
}
else{
console.log('Successful registration');
// create user's first page
// create a new page
console.log('username is '+req.session.user.username);
var cfg = {
title: 'index',
privacy: 'private',
owner: req.session.user.username,
allowed: [],
elements: []
}
console.log('about to create index page');
pageController.create(db, cfg, function(err, msg){
console.log('about to load index page with '+err+msg);
// load up the index
res.redirect('/'+req.session.user.username+'/index');
});
}
});
});
app.get('/sessions/destroy', function(req, res) {
delete req.session.user;
res.redirect('/sessions/new');
});
function renderPage(doc, res){
res.render('page', {
title: doc.owner+'\'s Pixlwiki - '+doc.title,
elements: doc.elements
});
}
app.get('/:username/:page?/:operation?', requiresLogin, function(req, res){
if(req.session.user.username !== req.params.username){
// user creating a page for another user
} else{
// user creating a page for themselves
var page = req.params.page;
var username = req.params.username;
// no page goes to index
if(!page){
page = 'index';
}
if(!req.params.operation){
// load a page
// use pageController to load mongo data
pageController.get(db, req, function(err, doc){
renderPage(doc, res);
});
} else if(req.params.operation === 'create'){
// create a new page
var cfg = {
title: page,
privacy: 'private',
owner: username,
allowed: [],
elements: []
}
pageController.create(db, cfg, function(err, msg){
pageController.get(db, req, function(err, doc){
renderPage(doc, res);
});
});
} else if(req.params.operation === 'delete'){
// delete a page
}
}
});
// update a page (ajax)
app.post('/:username/:page/update', requiresLogin, function(req, res){
// update an existing page
pageController.update(db, req, function(err,msg){
});
});
app.get('/', requiresLogin, function(req, res){
res.render('index', {
title: 'Pixlwiki'
});
});
app.get('/pixlwiki/about', function(req, res){
res.render('about', {
title: 'About Pixlwiki'
});
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);