Created
October 7, 2018 02:03
-
-
Save kidap/530c4a1a0a7328ec6b1fd02a7980d684 to your computer and use it in GitHub Desktop.
app.js
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 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'; | |
//Initialize 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:27017/passport-tutorial', { useNewUrlParser: true }); | |
mongoose.set('debug', true); | |
var db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error:')); | |
db.once('open', function callback () { | |
console.log("h"); | |
}); | |
// Models & routes | |
require('./models/Users'); | |
require('./config/passport'); | |
app.use(require('./routes')); | |
//Error handlers & middlewares | |
if (!isProduction) { | |
app.use((req, res, err) => { | |
console.log(res) | |
res.status(err.status || 500); | |
res.json({ | |
errors: { | |
message: err.message, | |
error: err, | |
}, | |
}); | |
}); | |
} | |
app.use((req, res, err) => { | |
console.log(res) | |
res.status(err.status || 500); | |
res.json({ | |
errors: { | |
message: err.message, | |
error: err, | |
}, | |
}); | |
}); | |
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