forked from angiecyborg/Pinterest-Clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
92 lines (76 loc) · 2.56 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
86
87
88
89
90
91
92
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const exphbs = require('express-handlebars');
const expressValidator = require('express-validator');
const cookieParser = require('cookie-parser');
const passport = require('passport');
const session = require('express-session');
const flash = require('connect-flash');
const multer = require('multer');
const methodOverride = require('method-override');
const { section, editOrDelete, formatDate, deleteComment } = require('./helpers/handlebars');
const keys = require('./config/keys');
// database connection
mongoose.Promise = global.Promise;
mongoose.connect(keys.mongoURI, { useMongoClient: true })
.then(() => console.log('Connection to MongoDB successful'))
.catch((err) => console.log(`Unable to connect to MongoDB: ${err}`));
// import models
const Pin = require('./models/pin');
const User = require('./models/user');
// initialize the express application
const app = express();
// import routes
const indexRoutes = require('./routes/index');
const authRoutes = require('./routes/auth');
const pinRoutes = require('./routes/pin');
// method override
app.use(methodOverride('_method'));
// view engine setup
app.engine('handlebars', exphbs({
defaultLayout: 'master',
helpers: { section, editOrDelete, formatDate, deleteComment }
}));
app.set('view engine', 'handlebars');
// serve favicon
app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon.ico')))
// static files
app.use(express.static(path.join(__dirname, 'public')));
// body parser & express validator
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator());
// sessions
app.use(session({
secret: keys.sessionSecret,
resave: true,
saveUninitialized: true
}));
// flash messages
app.use(flash());
// passport
require('./config/passport')(passport);
app.use(passport.initialize());
app.use(passport.session());
// global variables
app.use((req, res, next) => {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.loggedUser = req.user || null;
next();
});
// routes
app.use('/', indexRoutes);
app.use('/auth', authRoutes);
app.use('/pin', pinRoutes);
// handle 404
app.use((req, res) => {
res.status(404);
res.render('error', { title: '404: Page Not Found' });
});
// start the server
const PORT = process.env.PORT || 4444;
app.listen(PORT, () => console.log(`Application running on port ${ PORT }`));