-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
59 lines (52 loc) · 1.41 KB
/
index.ts
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
import dotenv from 'dotenv'
dotenv.config()
import express from 'express'
import expressWs from 'express-ws'
import cookieParser from 'cookie-parser'
import cors from 'cors'
import session from 'express-session'
import MongoStore from 'connect-mongo'
import passport from 'passport'
import path from 'path'
import history from 'connect-history-api-fallback'
import * as config from './server/config'
import './server/db' // Connect to database
import './server/models' // Register models
const PORT = process.env.PORT || 8080
const { app } = expressWs(express())
import routes from './server/routes'
import websockets from './server/websockets'
// Middleware
app.use(cookieParser())
app.use(cors({
credentials: true,
origin: config.clientUrl,
}))
app.use(express.json())
app.use(session({
secret: config.sessionSecret,
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: config.db.connectionString(),
}),
}))
import './server/passport'
app.use(passport.initialize())
app.use(passport.session())
app.use(routes)
app.use(websockets)
// Serve static assets
app.use(history())
app.use(express.static(path.join(__dirname + '/client')))
app.get('*', (req, res) => {
if (process.env.NODE_ENV === 'production') {
res.sendFile(path.resolve(__dirname, 'index.html'))
}
else {
res.redirect('localhost:8080')
}
})
app.listen(PORT, () => {
console.log(`listening on port ${PORT}`)
})