-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·129 lines (103 loc) · 2.83 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
/**
* Module dependencies.
*/
var express = require('express'),
mongoose = require('mongoose'),
User;
var app = module.exports = express.createServer();
require("./models/user");
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: "parp" }));
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.logger({ format: '\x1b[1m:method\x1b[0m \x1b[33m:url\x1b[0m :response-time ms' }))
});
app.configure('development', function(){
app.set('db-uri', 'mongodb://localhost/devdb');
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.set('db-uri', 'mongodb://localhost/prodb');
app.use(express.errorHandler());
});
app.configure('test', function() {
app.set('db-uri', 'mongodb://localhost/testdb');
});
db = mongoose.connect(app.set('db-uri'));
User = mongoose.model("User");
function requiresLogin(req, res, next) {
if (req.session.user) {
next();
} else {
res.redirect('sessions/new?redir=' + req.url);
}
};
// Users
User = mongoose.model('User');
app.get('/users/new', function(req, res) {
res.render('users/new.jade', {
title: 'Sign Up',
locals: { user: new User() }
});
});
app.post('/users', function(req, res) {
var user = new User(req.body.user);
function userSaveFailed() {
req.flash('error', 'Account creation failed');
console.log('user save failed');
res.render('users/new.jade', {
locals: { user: user }
});
}
user.save(function(err, u) {
if (err) {
console.log(err);
return userSaveFailed();
} else {
console.log(u);
req.session.user = u;
}
req.flash('info', 'Your account has been created');
res.redirect('/');
});
});
// Sessions
app.get('/sessions/new', function(req, res){
res.render('sessions/new', {
title: 'Login',
redir: req.query.redir
});
});
app.post('/sessions', function(req, res){
User.findOne({ email: req.body.username }, function(err, user) {
if (user && user.authenticate(req.body.password)) {
req.session.user_id = user.id;
} else {
req.flash('error', 'Incorrect credentials');
res.redirect('/sessions/new');
}
});
});
app.del('/sessions/new', requiresLogin, function(req, res){
if (req.session.user) {
req.session.user.destroy(function() {});
}
res.redirect('/sessions/new');
});
// Routes
app.get('/', requiresLogin, function(req, res){
res.render('index', {
title: 'Express'
});
});
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);
}