aboutsummaryrefslogblamecommitdiff
path: root/index.js
blob: b772ad2b7823e9452214359a791c7f4b94bcca48 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                   
                                             
                                     

                                                   
                                       
                                     
 
                                                           
                                                      
                                          
 
                      
 
                            



                           
 


                                                                                          
                 
                    


                                   
                      





                                        

                 


                                     

                                  

                                               



                               

                 
                        
 
                                                                       
const express = require('express');
const exphbs = require('express-handlebars');
const mongoose = require('mongoose');
const session = require('express-session');
const MongoSessionStore = require('connect-mongo');
const flash = require('connect-flash');
const passport = require('passport');

const {port, mongoUrl, masterKey} = require("./config/env")
const oldForm = require('./utils/middleware/oldForm');
const webRoutes = require('./routes/web');

const app = express();

mongoose.connect(mongoUrl, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useFindAndModify: false
});

mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.once('open', () => console.log("Successfully connected to MongoDB!"));

app.use(session({
  secret: masterKey,
  resave: false,
  saveUninitialized: true,
  store: MongoSessionStore.create({
    mongoUrl: mongoUrl
  }),
  cookie: {
    maxAge: 1000 * 60 * 60 * 24 // 1 day
  }
}));

app.use(flash());

app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');

app.use(express.static('public'));

app.use(express.urlencoded({extended: false}));

require('./config/passport');
app.use(passport.initialize());
app.use(passport.session());

app.use(oldForm);

app.use('/', webRoutes);

app.listen(port, () => console.log(`Server started on port ${port}.`));