-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
86 lines (68 loc) · 2.48 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// server.js
// set up ======================================================================
import express from 'express'
import mongoose from 'mongoose'
import passport from 'passport'
import flash from 'connect-flash'
import morgan from 'morgan'
import cookieParser from 'cookie-parser'
import bodyParser from 'body-parser'
import session from 'express-session'
import compression from 'compression'
import path from 'path'
import expressLayouts from 'express-ejs-layouts'
import cors from 'cors'
import swaggerUi from 'swagger-ui-express'
import YAML from 'yamljs'
import configDB from './config/database'
import passportConfig from './config/passport'
// routes ======================================================================
import routes from './app/routes'
import authRoutes from './app/authRoutes'
const app = express()
const swaggerDocument = YAML.load('./swagger.yaml')
// configuration ===============================================================
mongoose.connect(configDB.url) // connect to our database
passportConfig(passport) // pass passport for configuration
const rejectFolders = [
'css',
'bower_components',
'js',
'img',
'fonts',
'images']
// removing static resources from the logger
app.use(
morgan(':method :url :status :res[content-length] - :response-time ms', {
skip: req => rejectFolders.indexOf(req.url.split('/')[1]) !== -1,
}))
app.use(compression({threshold: 0}))
app.use(cookieParser()) // read cookies (needed for auth)
app.use(bodyParser.json({limit: '50mb', extended: true}))
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}))
app.use(cors())
app.set('view engine', 'ejs')
app.use(expressLayouts)
app.set('layout extractScripts', true)
app.set('layout extractStyles', true)
app.set('layout', 'layouts/main')
app.use(express.static(path.join(__dirname, 'public')))
// required for passport
app.use(session({
secret: process.env.SECRET_KEY || 'asdfsadfkjhsadfkjhasdkjfhasdjkfh k', // session secret
resave: true,
saveUninitialized: true,
}))
app.use(passport.initialize())
app.use(passport.session()) // persistent login sessions
app.use(flash()) // use connect-flash for flash messages stored in session
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument))
routes(app, passport) // load our routes and pass in our app and fully configured passport
app.use('/', authRoutes)
app.use((req, res, next) => {
const err = new Error('Not Found')
err.status = 404
next(err)
})
mongoose.set('debug', true)
module.exports = app