-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
105 lines (94 loc) · 2.65 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
93
94
95
96
97
98
99
100
101
102
103
104
105
const HTTP_PORT = process.env.PORT || 8080;
const express = require('express');
const app = express();
const router = require('./routes/index');
const mongoose = require('mongoose');
const handlebars = require('express-handlebars');
const multer = require('multer');
const expressValidator = require('express-validator');
const bodyParser = require('body-parser');
const connectFlash = require('connect-flash');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const passport = require('passport');
const methodOverride = require('method-override');
const path = require('path');
require('dotenv').config();
app.use(express.static('public'));
app.set('view engine', 'hbs');
app.engine(
'hbs',
handlebars({
extname: 'hbs',
defaultLayout: 'main',
layoutDir: __dirname + '/views/layouts',
partialsDir: path.join(__dirname, '/views/partials'),
helpers: {
ifThird: function (index, options) {
if (index % 3 === 0) {
return options.fn(this);
} else {
return options.inverse(this);
}
},
},
}),
);
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
});
mongoose.connection
.once('open', () => {
console.log('Successfully connected to MongdoDB using Mongoose!');
})
.on('error', (err) => {
console.log('MongoDB connection error: ', err);
});
app.use(
methodOverride('_method', {
methods: ['POST', 'GET'],
}),
);
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: true }));
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'public/uploads');
},
filename: (req, file, cb) => {
cb(null, Date.now() + '-' + file.originalname);
},
});
app.use(multer({ storage: storage }).single('photos'));
app.use(express.json());
app.use(expressValidator());
app.use(cookieParser(process.env.SECRET_KEY));
app.use(
session({
name: 'session',
secret: process.env.SECRET_KEY,
cookie: {
maxAge: 1000 * 60 * 23,
},
resave: false,
saveUninitialized: false,
}),
);
app.use(connectFlash());
app.use((req, res, next) => {
res.locals.currentUser = req.user;
res.locals.loggedIn = req.session.loggedIn;
res.locals.flashMessages = req.flash();
next();
});
app.use('/', router);
app.use((req, res) => {
res.status(404).send('Page Not Found');
});
app.listen(HTTP_PORT, () => {
console.log(`The server is running... to port: ${HTTP_PORT}`);
});