-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
186 lines (167 loc) · 3.89 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
// TODO: use connect-session for sessions
/* -== DEFINING ==- */
var express = require('express'),
app = express.createServer(),
mongoose = require('mongoose').Mongoose,
db = mongoose.connect('mongodb://localhost/blog-express-mongoose'),
pub = __dirname + '/public';
// db.dropDatabase - to drop DB;
mongoose.model('Post', {
properties: [
'title',
'slug',
'body',
{'comments': [
'person',
'comment',
'created_at'
]},
'created_at'
]
});
mongoose.model('User',{
properties: [
'name',
'password',
'member_since'
]
});
var Post = db.model('Post'),
User = db.model('User');
/* -== CONFIG ==- */
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyDecoder());
app.use(express.cookieDecoder());
//app.use(express.logger());
app.use(express.session());
app.use(app.router);
app.use(express.staticProvider(pub));
/* enable SASS */
app.use(express.compiler({ src: pub, enable: ['sass'] }));
/* templating engine */
app.set('view engine', 'jade');
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
/* -== APP ==- */
app.get('/', function(req, res) {
Post.find().sort([['date', 'descending']]).all(function(posts) {
res.render('blogs_index', {
locals : {
title : 'Blog',
articles: posts
}
})
});
});
app.get('/login', function(req, res) {
res.render('login', {
locals: {
title: 'Login',
error: null,
// where do we come from
redirectUri: req.flash('redirect-from')
}
});
});
app.post('/login', function(req, res) {
var name = req.param('login'),
pass = req.param('password'),
redirectUri = req.param('redirect-uri');
User.find({ name: name }).first(function(user){
// login matches db
if (user) {
// password matches
if (pass == user.password) {
// initilize session
req.session.user_id = user._id;
if (redirectUri && redirectUri != '') {
res.redirect(redirectUri);
}
else {
res.redirect('/');
}
}
// password doesn't match
else {
res.render('login', {
locals: {
title: 'Login',
error: {
type: 'password'
}
}
});
}
}
// specified name doesn't match anything in db
else {
res.render('login', {
locals: {
title: 'Login',
error: {
type: 'name',
data: req.param('login')
}
}
});
}
});
});
app.get('/logout', function(req, res) {
req.session.regenerate(function(err){
res.writeHead(302, { Location: '/' });
res.end();
});
});
app.get('/blog/new', function(req, res) {
if (userLoggedIn(req)) {
res.render('blog_new', {
locals: {
title: 'New Post'
}
});
}
else {
req.flash('redirect-from', req.url);
res.redirect('/login');
}
});
app.post('/blog/new', function(req, res) {
new Post({
title: req.param('title'),
slug: req.param('slug'),
body: req.param('body'),
created_at: new Date()
}).save(function(){
res.redirect('/');
});
});
app.get('/blog/:slug', function(req, res){
Post.find({ slug: req.params.slug }).first(function(post) {
res.render('blog_post', {
locals: {
title: 'Blog',
post: post
}
});
});
});
function userLoggedIn(req) {
var id = req.session.user_id || '';
if (id != '') {
return function() {
User.find({ _id: id }).first(function(user) {
return user ? true : false;
});
}
}
}
/* -== RUNNING SERVER ==- */
app.listen(3000);
console.log('Express server started on port %s', app.address().port);