Created
May 28, 2018 13:19
-
-
Save neenjaw/52a16e3011e0c4c2e8bff36bc5c5739b to your computer and use it in GitHub Desktop.
Starter - Node/Express/MongoDB/Mongoose/EJS/Passport
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
// ============================ | |
// Node Requires | |
// ============================ | |
const express = require('express'); | |
const mongoose = require('mongoose'); | |
// const flash = require('express-flash-2'); // flash messaging helper library | |
// const methodOverride = require('method-override'); // allow UPDATE and DELETE to be routed from POST forms | |
// const expressSanitizer = require('express-sanitizer'); // sanitize input for injection protection | |
// const passport = require('passport'); | |
// const LocalStrategy = require('passport-local'); | |
// const passportLocalMongoose = require('passport-local-mongoose'); | |
// const reload = require('reload'); // inject hot reloading browser when changes occur for development | |
// ============================ | |
// Route Includes | |
// ============================ | |
// Refactor routes out to here | |
// const indexRoutes = require('./routes/index'); | |
// const userRoutes = require('./routes/user'); | |
// const campgroundRoutes = require('./routes/campground'); | |
// const commentRoutes = require('./routes/comment'); | |
// ============================ | |
// Mongoose Connect | |
// ============================ | |
const dbProtocol = 'mongodb'; | |
const dbUser = process.env.MONGO_DB_USER || ''; | |
const dbPassword = process.env.MONGO_DB_PASSWORD || ''; | |
const dbUrl = process.env.MONGO_DB_URL || 'localhost/test'; | |
const dbCredentialsProvided = (dbUser && dbPassword) ? true : false; | |
if (dbCredentialsProvided) { | |
mongoose.connect(`${dbProtocol}://${dbUser}:${dbPassword}@${dbUrl}`); | |
} else { | |
mongoose.connect(`${dbProtocol}://${dbUrl}`); | |
} | |
// ============================ | |
// Mongoose Schema Requires | |
// ============================ | |
// Move Models out, import them here | |
// const User = require('./models/user'); | |
// const Campground = require('./models/campground'); | |
// const Comment = require('./models/comment'); | |
// const seed = require('./seed'); | |
// seed.seedDB(); | |
// ============================ | |
// Express Setup | |
// ============================ | |
const app = express(); | |
app.set('ip', process.env.IP || 'localhost'); | |
app.set('port', process.env.PORT || 3000); | |
// View Engine | |
app.set('view engine', 'ejs'); | |
// Body Parsing | |
app.use(express.urlencoded({extended: true})); | |
// Sanitize the incoming body | |
// app.use(expressSanitizer()); | |
// Static File Serve Dir | |
app.use(express.static(__dirname + '/public')); | |
// Method Override | |
// app.use(methodOverride('_method')); | |
// Session | |
app.use(require('express-session')({ | |
secret: 'n33BjOLuNncxtuYXpbux', | |
resave: false, | |
saveUninitialized: false, | |
})); | |
// app.use(flash()); | |
// app.locals.moment = require('moment'); allow use of moment library client side | |
// ============================ | |
// Passport Setup | |
// ============================ | |
// Startup Passport | |
// app.use(passport.initialize()); | |
// app.use(passport.session()); | |
// Config Passport | |
// passport.use(new LocalStrategy(User.authenticate())); // -- maybe unnecessary | |
// passport.use(User.createStrategy()); | |
// passport.serializeUser(User.serializeUser()); | |
// passport.deserializeUser(User.deserializeUser()); | |
// ============================ | |
// Misc Setup | |
// ============================ | |
// Include the user data in each response for the render template | |
app.use((req, res, next) => { | |
if (typeof req.user === 'undefined') { | |
res.locals.user = undefined; | |
} else { | |
res.locals.user = { | |
id: req.user._id, | |
username: req.user.username, | |
name: req.user.displayName, | |
isAdmin: req.user.isAdmin | |
}; | |
} | |
next(); | |
}); | |
// ============================ | |
// Routes | |
// ============================ | |
// app.use(indexRoutes); | |
// app.use('/users', userRoutes); | |
// app.use('/campgrounds', campgroundRoutes); | |
// app.use('/campgrounds/:id/comments', commentRoutes); | |
// ============================ | |
// Server Start / Listen | |
// ============================ | |
// reload(app); | |
app.listen(app.get('port'), app.get('ip'), () => { | |
console.log('Server starting on '+app.get('ip')+':'+app.get('port')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment