Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Quiz/app.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
61 lines (48 sloc)
1.33 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const expressLayouts = require('express-ejs-layouts'); | |
const mongoose = require('mongoose'); | |
const passport = require('passport'); | |
const flash = require('connect-flash'); | |
const session = require('express-session'); | |
const app = express(); | |
// Passport Config | |
require('./config/passport')(passport); | |
// DB Config | |
const db = require('./config/keys').mongoURI; | |
// Connect to MongoDB | |
mongoose.connect( | |
db, | |
{ useNewUrlParser: true } | |
) | |
.then(() => console.log('MongoDB Connected')) | |
.catch(err => console.log(err)); | |
// EJS | |
app.use(expressLayouts); | |
app.set('view engine', 'ejs'); | |
// Express body parser | |
app.use(express.urlencoded({ extended: true })); | |
// Express session | |
app.use( | |
session({ | |
secret: 'secret', | |
resave: true, | |
saveUninitialized: true | |
}) | |
); | |
// Passport middleware | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
// Connect flash | |
app.use(flash()); | |
// Global variables | |
app.use(function(req, res, next) { | |
res.locals.success_msg = req.flash('success_msg'); | |
res.locals.error_msg = req.flash('error_msg'); | |
res.locals.error = req.flash('error'); | |
next(); | |
}); | |
// Routes | |
app.use('/', require('./routes/index.js')); | |
app.use('/users', require('./routes/users.js')); | |
const PORT = process.env.PORT || 5000; | |
app.listen(PORT, console.log(`Server started on port ${PORT}`)); |