-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (51 loc) · 1.64 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
const prod = process.env.NODE_ENV === 'production'
if (!prod) {
require('dotenv').config()
}
const express = require('express');
const app = express();
const expressLayouts = require('express-ejs-layouts');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const methodOverride = require('method-override');
const session = require('express-session')
const {setUser, isLoggedIn} = require('./middleware/sessionMiddleware');
const indexRouter = require('./routes/index');
const loginRouter = require('./routes/login');
const tripRouter = require('./routes/trip');
//View setup
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.set('layout', 'layouts/layout');
app.use(expressLayouts);
app.use(express.static('public'));
app.use(methodOverride('_method'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//session setup
app.use(cookieParser());
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
secure: prod,
sameSite: prod ? 'none' : 'lax',
maxAge: parseInt(process.env.SESSION_MAX_AGE),
},
proxy: prod,
}));
//DB setup
const mongoose = require('mongoose');
mongoose.set('strictQuery', false);
mongoose.connect(process.env.DATABASE_URL);
const db = mongoose.connection;
db.on('error', error => console.error(error));
db.once('open', () => console.log('Connected to Mongoose'));
app.use(setUser)
app.use('/', indexRouter);
app.use('/auth', loginRouter)
app.use(isLoggedIn)
app.use('/trips', tripRouter)
app.listen(process.env.PORT || 3000);