-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
255 lines (255 loc) · 7.14 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* Module dependencies.
*/
var express = require('express'),
app = module.exports = express.createServer(),
mongoose = require('mongoose'),
db = mongoose.connect('mongodb://spoeken:[email protected]:33457/acms'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId,
passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
ensureAuthenticated;
//
var users = [
{ id: 1, username: 'mathias', password: 'hemmelig', email: '[email protected]' },
{ id: 2, username: 'eivind', password: 'hemmelig', email: '[email protected]' }
];
function findById(id, fn) {
"use strict";
var idx = id - 1;
if (users[idx]) {
fn(null, users[idx]);
} else {
fn(new Error('User ' + id + ' does not exist'));
}
}
function findByUsername(username, fn) {
"use strict";
var i,
len,
user;
for (i = 0, len = users.length; i < len; i += 1) {
user = users[i];
if (user.username === username) {
return fn(null, user);
}
}
return fn(null, null);
}
passport.serializeUser(function (user, done) {
"use strict";
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
"use strict";
findById(id, function (err, user) {
done(err, user);
});
});
passport.use(new LocalStrategy(
function (username, password, done) {
"use strict";
// asynchronous verification, for effect...
process.nextTick(function () {
findByUsername(username, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
if (user.password !== password) { return done(null, false, { message: 'Invalid password' }); }
return done(null, user);
});
});
}
));
//
app.configure(function () {
'use strict';
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(function (req, res, next) {
res.locals.url = req.url;
next();
});
app.use(function (req, res, next) {
res.locals.host = 'http://' + req.headers.host;
res.locals.query = req.query;
next();
});
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function () {
'use strict';
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function () {
'use strict';
app.use(express.errorHandler());
});
var posts = new Schema({
author : ObjectId,
title : String,
slug : String,
content : String,
type : String,
buf : Buffer,
date : Date
});
var post = db.model('post', posts);
/*----------------------------/
READ
---------------------------*/
function getPostBySlug($slug, $cb) {
'use strict';
post.find({slug: $slug}, function (err, data) {
data.forEach(function (elem, index, array) {
$cb(elem);
});
});
}
function getAllPosts($cb) {
'use strict';
post.find({type: "post"}, function (err, data) {
if (!err) {
$cb(data);
} else {
console.log(err);
}
});
}
//
app.get('/', function (req, res) {
'use strict';
res.redirect('/blog/');
});
/*----------------------------/
LOGIN
---------------------------*/
app.get('/logout', function (req, res) {
'use strict';
req.logout();
res.redirect('/');
});
app.get('/login', function (req, res) {
'use strict';
res.render('login', {page: {title: "Login"}});
});
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login?error=Username and/or password incorrect', failureFlash: false }),
function (req, res) {
'use strict';
res.redirect('/admin/posts');
});
/*----------------------------/
---------------------------*/
//Delete Post by
app.post('/admin/posts/delete', ensureAuthenticated, function (req, res) {
"use strict";
console.log(req.body);
post.remove({_id: req.body.post_id}, function (err) {
if (!err) {
res.redirect('admin/posts?warning=The Post was deleted');
} else {
res.redirect('admin/posts?warning=Could not delete post. Error: ' + err);
}
});
});
/*----------------------------/
---------------------------*/
//Add New
app.get('/admin/posts/addnew', ensureAuthenticated, function (req, res) {
'use strict';
res.render('addPost', {page: {title: "Add New Post", content: "Add a new post"}});
});
// Save New
app.post('/admin/posts/addnewtodb', ensureAuthenticated, function (req, res) {
'use strict';
if (req.body.title !== "") {
var instance = new post();
instance.title = req.body.title;
instance.slug = req.body.slug;
instance.content = req.body.content;
instance.type = "post";
instance.date = new Date();
instance.save(function (err) {
if (!err) {
res.redirect('admin/posts/addnew?warning=The post was successfully created!');
} else {
res.redirect('admin/posts/addnew?warning=Could not create post! Error:' + err);
}
});
} else {
res.redirect('admin/posts/addnew?warning=Missing title!');
}
});
/*----------------------------/
---------------------------*/
//Edit
app.get('/admin/posts/edit', ensureAuthenticated, function (req, res) {
'use strict';
post.findOne({_id: req.query.post_id}, function (err, data) {
console.log(data);
res.render('editPost', {page: {title: "Edit Post", content: "Edit the post"}, post: data});
});
});
//Save Edited
app.post('/admin/posts/edit/save', ensureAuthenticated, function (req, res) {
'use strict';
post.findOne({_id: req.body.post_id}, function (err, data) {
data.title = req.body.title;
data.slug = req.body.slug;
data.content = req.body.content;
data.save(function (err) {
if (!err) {
res.redirect('admin/posts/edit?post_id=' + req.body.post_id + '&warning=The post was successfully updated!');
} else {
res.redirect('admin/posts/edit?post_id=' + req.body.post_id + '&warning=Could not save post! Error:' + err);
}
});
});
});
/*----------------------------/
READ
---------------------------*/
// Show all
app.get('/admin/posts', ensureAuthenticated, function (req, res) {
'use strict';
getAllPosts(function (data) {
res.render('posts', {page: {title: "Posts" }, blogposts: data});
});
});
app.get('/blog', function (req, res) {
'use strict';
getAllPosts(function (data) {
res.render('blog', {page: {title: "a-cms blog", header: "Welcome to the a-cms blog. It is super effective at teaching you how to use a-cms" }, blogposts: data});
});
});
//Single
app.get('/blog/:slug', function (req, res) {
'use strict';
getPostBySlug(req.params.slug, function (data) {
res.render('singlepost', {page: data});
});
});
var port = process.env.PORT || 20095;
app.listen(port);
console.log('Express server listening on port %d in %s mode', port, app.settings.env);
// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected.
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to
// login page.
function ensureAuthenticated(req, res, next) {
'use strict';
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login?warning=You have to be logged in to access that area');
}