forked from FabricLabs/maki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (76 loc) · 2.81 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
var express = require('express')
, app = express()
, mongoose = require('mongoose')
, passport = require('passport')
, LocalStrategy = require('passport-local').Strategy
, passportLocalMongoose = require('passport-local-mongoose')
, config = require('./config')
, database = require('./db');
/* Models represent the data your application keeps. */
/* You'll need at least the User model if you want to
allow users to login */
User = require('./models/User').User;
//Thing = require('./models/Thing').Thing;
// make the HTML output readible, for designers. :)
app.locals.pretty = true;
// any files in the /public directory will be accessible over the web,
// css, js, images... anything the browser will need.
app.use(express.static(__dirname + '/public'));
app.engine('jade', require('jade').__express);
// set up middlewares for session handling
app.use(express.cookieParser( config.cookieSecret ));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({
secret: config.cookieSecret
}));
/* Configure the registration and login system */
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.set('view engine', 'jade');
/* configure some local variables for use later */
app.use(function(req, res, next) {
app.locals.user = req.user;
next();
});
passport.use(new LocalStrategy( User.authenticate() ) );
passport.serializeUser( User.serializeUser() );
passport.deserializeUser( User.deserializeUser() );
/* Configure "routes".
"routes" are the mappings your browser/client use to
access the logic behind a concept. Google "REST" to
learn more about the principle. */
/* the first route we'll configure is our front page. :) */
/* this means: when a GET request is issued to (yourapp)/,
...execute a function with the [req]uest, [res]ponse, and
the [next] function. */
app.get('/', function(req, res) {
/* in this function, render the index template,
using the [res]ponse. */
res.render('index', {
foo: 'bar' //this is an example variable to be sent to the rendering engine
});
});
app.get('/register', function(req, res) {
res.render('register');
});
app.get('/login', function(req, res) {
res.render('login');
});
/* when a POST request is made to '/register'... */
app.post('/register', function(req, res) {
User.register(new User({ email : req.body.email, username : req.body.username }), req.body.password, function(err, user) {
if (err) {
console.log(err);
return res.render('register', { user : user });
}
res.redirect('/');
});
});
app.post('/login', passport.authenticate('local'), function(req, res) {
res.redirect('/');
});
app.listen( config.appPort , function() {
console.log('Demo application is now listening on http://localhost:' + config.appPort + ' ...');
});