Created
August 28, 2018 09:50
-
-
Save devarajchidambaram/29f2b7fb327e88ab15b8540cafd15302 to your computer and use it in GitHub Desktop.
sample app
This file contains hidden or 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 path = require('path'); | |
const bodyParser = require('body-parser'); | |
const session = require('express-session'); | |
const cors = require('cors'); | |
const mongoose = require('mongoose'); | |
const errorHandler = require('errorhandler'); | |
//Configure mongoose's promise to global promise | |
mongoose.promise = global.Promise; | |
//Configure isProduction variable | |
const isProduction = process.env.NODE_ENV === 'production'; | |
//Initiate our app | |
const app = express(); | |
//Configure our app | |
app.use(cors()); | |
app.use(require('morgan')('dev')); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.json()); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
app.use(session({ secret: 'passport-tutorial', cookie: { maxAge: 60000 }, resave: false, saveUninitialized: false })); | |
if(!isProduction) { | |
app.use(errorHandler()); | |
} | |
//Configure Mongoose | |
mongoose.connect('mongodb://localhost/passport-tutorial'); | |
mongoose.set('debug', true); | |
//Error handlers & middlewares | |
if(!isProduction) { | |
app.use((err, req, res) => { | |
res.status(err.status || 500); | |
res.json({ | |
errors: { | |
message: err.message, | |
error: err, | |
}, | |
}); | |
}); | |
} | |
app.use((err, req, res) => { | |
res.status(err.status || 500); | |
res.json({ | |
errors: { | |
message: err.message, | |
error: {}, | |
}, | |
}); | |
}); | |
app.listen(8000, () => console.log('Server running on http://localhost:8000/')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment