generated from mgcarbonell/pern-auth-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
52 lines (42 loc) · 1.33 KB
/
server.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
// imports
require('dotenv').config();
const express = require('express')
const cors = require('cors')
const session = require('express-session')
const morgan = require('morgan')
const routes = require('./routes')
const passport = require('./passport')
const port = process.env.PORT || 4000
const app = express()
// middleware - server logging
app.use(morgan('dev'))
// middleware - JSON parsing
app.use(express.json())
// middleware - cors
const corsOptions = {
// from which URLs do we want to accept requests
origin: ['CLIENT_URL'],
credentials: true, // allow the session cookie to be sent to and from the client
optionsSuccessStatus: 204
}
app.use(cors(corsOptions))
// middleware - session config
app.use(session({
// session is stored in the DB
secret: process.env.SESSION_SECRET,
resave: false, // will not resave sessions
saveUninitialized: false, // only create a session when a property is added to the session
cookie: {
maxAge: 1000 * 60 * 60 * 24
}
}))
// middleware - passport config
app.use(passport.initialize())
app.use(passport.session())
// middleware - API routes
app.use('/api/v1/auth', routes.auth)
app.use('/api/v1/entry', routes.entry)
app.use('/api/v1/comment', routes.comment)
app.use('/api/v1/prompt', routes.prompt)
// connection
app.listen(port, () => console.log(`Server is running on port ${port}`))