-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
96 lines (83 loc) · 2.48 KB
/
server.js
File metadata and controls
96 lines (83 loc) · 2.48 KB
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
88
89
90
91
92
93
94
95
96
const config = require('./config.json')
const express = require('express')
const pg = require('pg')
const knex = require('knex')(config.knex)
const path = require('path')
const session = require('express-session')
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const helmet = require('helmet')
// const redis = require('redis')
const port = process.env.PORT || 8080
const env = process.env.NODE_ENV
const app = express()
// const client = redis.createClient()
const { secret } = require('./secret.json')
const staticPath = path.join(__dirname, 'static')
const ws = require('./utils/websockets')(knex)
const comment = require('./routes/comment')(knex)
const profile = require('./routes/profile')(knex)
const stock = require('./routes/stock')(knex)
const trade = require('./routes/trade')(knex)
const user = require('./routes/user')(knex)
const whiteList = [
'http://localhost:3000',
'http://162.243.58.89:8080',
'http://quantblitz.com:8080',
'http://162.243.58.89:4040',
'http://quantblitz.com:4040',
'ws://162.243.58.89:4040',
'ws://www.quantblitz.com:4040',
'http://162.243.58.89',
'http://quantblitz.com'
]
const corsOptionsDelegate = (req, res, next) => {
res.header('Access-Control-Allow-Credentials', true)
res.header('Access-Control-Allow-Headers', 'Content-Type')
res.header('Access-Control-Allow-Methods', 'OPTIONS,GET,PUT,POST,DELETE')
if (whiteList.indexOf(req.headers.origin) > -1) {
res.setHeader('Access-Control-Allow-Origin', req.headers.origin)
}
next()
}
app.use(corsOptionsDelegate)
app.use(helmet())
app.use(bodyParser.json())
app.use(cookieParser())
app.use(session({
secret,
name: 'sessionCookieID',
resave: false,
saveUninitialized: true,
cookie: {
expires: new Date(Date.now() + 3600000), // 1 Hour
maxAge: 3600000
}
}))
// Loading of routes
app.use('/v1/comment', comment)
app.use('/v1/profile', profile)
app.use('/v1/stock', stock)
app.use('/v1/trade', trade)
app.use('/v1/user', user)
// Refresh sessions
app.use('/v1/', (req, res, next) => {
if (req.session.userID) {
const { email, username } = req.session
res.status(200).send({
user: { email, username }
})
} else {
res.status(401).send()
}
})
// Server-side rendering for React
if (env === 'production') {
app.use(express.static(staticPath))
app.get('*', (req, res) => {
res.sendFile('index.html', {
root: staticPath
})
})
}
app.listen(port, () => console.log('λ CORS-enabled server on port:', port))