-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.js
226 lines (193 loc) · 5.24 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
219
220
221
222
223
224
/**
* Module dependencies.
*/
var express = require('express')
, gzippo = require('gzippo')
, routes = require('./routes')
, crypto = require('crypto')
, moment = require('moment')
, cluster = require('cluster')
, os = require('os')
, db = require('mongojs').connect('blog', ['post', 'user']);
var conf = {
salt: 'rdasSDAg'
};
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
//app.use(express.logger());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'wasdsafeAD' }));
app.use(gzippo.staticGzip(__dirname + '/public'));
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
//app.use(express.errorHandler());
});
app.helpers({
moment: moment
});
app.dynamicHelpers({
user: function(req, res) {
return req.session.user;
},
flash: function(req, res) {
return req.flash();
}
});
// Routes
function isUser(req, res, next) {
if (req.session.user) {
next();
} else {
next(new Error('You must be user to access this page'));
}
}
app.error(function(err, req, res, next){
if (err instanceof NotFound) {
res.render('error/404.jade', { title: 'Not found 404' });
} else {
res.render('error/500.jade', { title: 'Error', error: err });
}
});
// Listing
app.get('/', function(req, res) {
var fields = { subject: 1, body: 1, tags: 1, created: 1, author: 1 };
db.post.find({ state: 'published'}, fields).sort({ created: -1}, function(err, posts) {
if (!err && posts) {
res.render('index.jade', { title: 'New era blog', postList: posts });
}
});
});
app.get('/post/add', isUser, function(req, res) {
res.render('add.jade', { title: 'Add new blog post '});
});
app.post('/post/add', isUser, function(req, res) {
var values = {
subject: req.body.subject
, body: req.body.body
, tags: req.body.tags.split(',')
, state: 'published'
, created: new Date()
, modified: new Date()
, comments: []
, author: {
username: req.session.user.user
}
};
db.post.insert(values, function(err, post) {
console.log(err, post);
res.redirect('/');
});
});
// Show post
// Route param pre condition
app.param('postid', function(req, res, next, id) {
if (id.length != 24) throw new NotFound('The post id is not having correct length');
db.post.findOne({ _id: db.ObjectId(id) }, function(err, post) {
if (err) return next(new Error('Make sure you provided correct post id'));
if (!post) return next(new Error('Post loading failed'));
req.post = post;
next();
});
});
app.get('/post/edit/:postid', isUser, function(req, res) {
res.render('edit.jade', { title: 'Edit post', blogPost: req.post } );
});
app.post('/post/edit/:postid', isUser, function(req, res) {
db.post.update({ _id: db.ObjectId(req.body.id) }, {
$set: {
subject: req.body.subject
, body: req.body.body
, tags: req.body.tags.split(',')
, modified: new Date()
}}, function(err, post) {
if (!err) {
req.flash('info', 'Post has been sucessfully edited');
}
res.redirect('/');
});
});
app.get('/post/delete/:postid', isUser, function(req, res) {
db.post.remove({ _id: db.ObjectId(req.params.postid) }, function(err, field) {
if (!err) {
req.flash('error', 'Post has been deleted');
}
res.redirect('/');
});
});
app.get('/post/:postid', function(req, res) {
res.render('show.jade', {
title: 'Showing post - ' + req.post.subject,
post: req.post
});
});
// Add comment
app.post('/post/comment', function(req, res) {
var data = {
name: req.body.name
, body: req.body.comment
, created: new Date()
};
db.post.update({ _id: db.ObjectId(req.body.id) }, {
$push: { comments: data }}, { safe: true }, function(err, field) {
if (!err) {
req.flash('success', 'Comment added to post');
}
res.redirect('/');
});
});
// Login
app.get('/login', function(req, res) {
res.render('login.jade', {
title: 'Login user'
});
});
app.get('/logout', isUser, function(req, res) {
req.session.destroy();
res.redirect('/');
});
app.post('/login', function(req, res) {
var select = {
user: req.body.username
, pass: crypto.createHash('sha256').update(req.body.password + conf.salt).digest('hex')
};
db.user.findOne(select, function(err, user) {
if (!err && user) {
// Found user register session
req.session.user = user;
res.redirect('/');
} else {
// User not found lets go through login again
res.redirect('/login');
}
});
});
//The 404
app.get('/*', function(req, res){
throw new NotFound;
});
function NotFound(msg){
this.name = 'NotFound';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
/**
* Adding the cluster support
*/
if (cluster.isMaster) {
// Be careful with forking workers
for (var i = 0; i < os.cpus().length * 1; i++) {
var worker = cluster.fork();
}
} else {
// Worker processes
app.listen(3000);
}