-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
87 lines (63 loc) · 2.03 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
87
const express = require('express')
const path = require('path')
const cookieParser = require('cookie-parser')
const logger = require('morgan')
const cors = require('cors')
const helmet = require('helmet')
const nocache = require('nocache')
const session = require('express-session')
const middleware = require('./util/middleware')
const variables = require('./util/variables')
const userRouter = require('./routes/user')
const blogRouter = require('./routes/blog')
const authorRouter = require('./routes/author')
const readinglistRouter = require('./routes/readinglist')
const app = express()
// if run behind a proxy (e.g. nginx)
// app.set('trust proxy', 1);
// for dev only
const db = require('./models')
app.use(cookieParser())
app.use(express.json())
app.use(logger('dev'))
app.use(express.urlencoded({ extended: false }))
app.use(
cors({
origin: ['http://localhost:3000'],
})
)
app.disable('x-powered-by')
app.use(helmet())
app.use(nocache())
app.use(
session({
store: new (require('connect-pg-simple')(session))({
createTableIfMissing: true,
}),
secret: [variables.cookie_secret1, variables.cookie_secret2],
name: variables.cookie_name,
resave: false,
saveUninitialized: false,
cookie: {
secure: process.env.NODE_ENV === 'production' ? true : false, // if true only transmit cookie over https
httpOnly: false, // if true prevent client side JS from reading the cookie
maxAge: 1000 * 60 * 600, // session max age in miliseconds (1 hr)
sameSite: 'lax',
},
})
)
app.use(express.static('build'))
app.use(express.static(path.join(__dirname, 'public')))
if (process.env.NODE_ENV === 'development') {
db.sequelize.sync({ force: true }).then(() => {
console.log('db has been re sync')
})
}
app.use('/api/users', userRouter)
app.use('/api/blogs', blogRouter)
app.use('/api/authors', authorRouter)
app.use('/api/readinglists', readinglistRouter)
app.use(require('sanitize').middleware)
app.use(middleware.endPoint404)
app.use(middleware.errorHandler)
module.exports = app