This repository has been archived by the owner on Mar 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
85 lines (75 loc) · 3.18 KB
/
server.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
// server.js
// set up ======================================================================
// get all the tools we need
var express = require('express');
var fileUpload = require('express-fileupload');
var exphbs = require('express-handlebars');
var app = express();
var port = process.env.PORT || 8080;
// DB and Passport configuration ===============================================================
var mongoose = require('mongoose');
var configDB = require('./config/database.js');
mongoose.connect(configDB.url); // connect to our database
// get project information
var package = require('./package.json');
app.title = package.title;
var flash = require('connect-flash');
var session = require('express-session');
var passport = require('passport');
require('./config/passport')(passport); // pass passport for configuration
//general express stuff
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(methodOverride()); // simulate DELETE and PUT
app.use(fileUpload());
// required for passport
app.use(session({ secret: 'insert_secret_here' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// create handlebars instance with exphbs
var hbs = exphbs.create({
defaultLayout: 'main',
extname: '.hbs',
layoutsDir:"views/layouts/",
partialsDir:["views/partials/","node_modules/hbs-header/views/partials/"],
helpers: {
ifCond: function (v1, operator, v2, options) {
switch (operator) {
case '==':
return (v1 == v2) ? options.fn(this) : options.inverse(this);
case '===':
return (v1 === v2) ? options.fn(this) : options.inverse(this);
case '<':
return (v1 < v2) ? options.fn(this) : options.inverse(this);
case '<=':
return (v1 <= v2) ? options.fn(this) : options.inverse(this);
case '>':
return (v1 > v2) ? options.fn(this) : options.inverse(this);
case '>=':
return (v1 >= v2) ? options.fn(this) : options.inverse(this);
case '&&':
return (v1 && v2) ? options.fn(this) : options.inverse(this);
case '||':
return (v1 || v2) ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}
}
}//end helpers
});
app.engine('hbs', hbs.engine); //define hbs
app.set('view engine', 'hbs'); //use hbs
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use('/users', express.static(__dirname + '/public')); //use index.css for login logout pages
// set up our routes
require('./app/routes.js')(app); // use "/" from own /app/routes.js
require('hbs-header')(app,session,passport);
app.listen(port);
console.log('The magic happens on port ' + port);