-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
54 lines (43 loc) · 1.52 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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const cookieSession = require('cookie-session');
const passport = require('passport');
const authRoutes = require('./routes/auth-routes');
const profileRoutes = require('./routes/profile-routes');
const passportSetup = require('./config/passport-setup');
const mongoose = require('mongoose');
const port = process.env.PORT || 3000;
const enforce = require('express-sslify');
const app = express();
if (process.env.NODE_ENV == "production") app.use(enforce.HTTPS({ trustProtoHeader: true }));
// Other needed Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.static(__dirname + '/public'));
// set view engine
app.set('view engine', 'ejs');
// set up session cookies
app.use(cookieSession({
maxAge: 24 * 60 * 60 * 1000,
keys: [process.env.SESSION_COOKIE_KEY]
}));
// initialize passport
app.use(passport.initialize());
app.use(passport.session());
// connect to mongodb
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true }, () => {
console.log('connected to mongodb');
});
// set up routes
app.use('/auth', authRoutes);
app.use('/profile', profileRoutes);
// create home route
app.get('/', (req, res) => {
res.render('home', { user: req.user });
});
app.listen(port, () => {
console.log(`Running on port ${ port }, localhost: http://localhost:${ port }/ `);
});