-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
68 lines (59 loc) · 2 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
// include modules
require('dotenv').config(); // loads the .env
const bodyParser = require('body-parser');
const cloudinary = require('cloudinary');
const db = require('./models');
const express = require('express');
const expressLayouts = require('express-ejs-layouts');
const flash = require('connect-flash');
const isLoggedIn = require('./middleware/isLoggedIn');
const logger = require('morgan');
const mongoose = require('mongoose');
const multer = require('multer');
const passport = require('./config/passportConfig');
const path = require('path');
const session = require('express-session');
const upload = multer({ dest: './uploads/' });
// initialize app
const app = express();
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/honeydew');
/* error logger, static routes */
app.use(logger('dev'));
app.use(express.static('public'));
app.use('/static', express.static(path.join(__dirname, 'public')));
// set and use statements. set view engine and use middleware.
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressLayouts);
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
// maintain active user
app.use(function(req, res, next) {
res.locals.currentUser = req.user;
res.locals.alerts = req.flash();
next();
});
// top-level route
app.get('/', (req, res) => {
res.render('home')
});
// include routes from controllers
app.use('/recipes', require('./controllers/recipes'));
app.use('/auth', require('./controllers/auth'));
app.use('/grocerylist', require('./controllers/grocerylist'));
app.use('/profile', require('./controllers/profile'));
/* Error Handling */
app.get('*', function (req, res) {
res.status(404).send('This is not the page you are looking for')
});
/* Listen on PORT */
app.set('port', process.env.PORT || 3001)
app.listen(app.get('port'), () => {
console.log(`Listening on port: ${app.get('port')}`)
})