-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.js
64 lines (59 loc) · 1.91 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
/* eslint-disable prefer-destructuring */
require('dotenv').config();
const path = require('path');
const express = require('express');
const mongoose = require('mongoose');
const methodOverride = require('method-override');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const flash = require('connect-flash');
const cors = require('cors');
const MongoStore = require('connect-mongo')(session);
const router = require('./routes/index');
// Database Connection
mongoose.connect(process.env.DATABASE_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
});
const db = mongoose.connection;
db.on('error', (error) => console.error(error));
db.once('open', () => console.log('Connected to Database 😍'));
// App Engine
const app = express();
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use('public', express.static('public'));
app.use(cors());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(methodOverride('_method'));
app.use(cookieParser());
//app.use(session({ secret: 'maktro-ms', saveUninitialized: true, resave: true }));
app.use(
session({
secret: process.env.SECRET_KEY,
saveUninitialized: false,
resave: false,
cookie: { maxAge: 1000 * 60 * 60 * 24 * 7, httpOnly: true },
store: new MongoStore({
mongooseConnection: db,
collection: 'session',
}),
})
);
app.use(flash());
app.use((req, res, next) => {
const successFlashMessageArr = req.flash('success');
const errorFlashMessageArr = req.flash('error');
res.locals.successMsg = successFlashMessageArr[0];
res.locals.errorMsg = errorFlashMessageArr[0];
next();
});
app.use('/', router);
// Development Server
const PORT = process.env.PORT || 5000; //
app.listen(PORT, () => {
console.log(`Application started on port: http://localhost:${PORT} 🔥`);
});