-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
52 lines (42 loc) · 1.32 KB
/
server.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
import express, { Express } from 'express'
import { json } from 'body-parser'
import expressSession from 'express-session'
import path from 'path'
import cors from 'cors'
import dotenv from 'dotenv'
import codeBlockRoutes from './api/codeBlock/codeBlockRoutes'
import socketService from './services/socketService'
import authRoutes from './api/auth/authRoutes'
import userRoutes from './api/user/userRoutes'
dotenv.config()
const app: Express = express()
const http = require('http').createServer(app)
const session = expressSession({
secret: 'secret session',
resave: false,
saveUninitialized: true,
cookie: { secure: false },
})
app.use(session)
app.use(json())
app.use(express.static('public'))
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.resolve(__dirname, 'public')))
} else {
const corsOptions = {
origin: ['http://127.0.0.1:3000', 'http://localhost:3000'],
credentials: true,
}
app.use(cors(corsOptions))
}
app.use('/api/auth', authRoutes)
app.use('/api/user', userRoutes)
app.use('/api/codeBlock', codeBlockRoutes)
socketService.connectSockets(http, session)
app.get('/**', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'))
})
const PORT = process.env.PORT || 3030
http.listen(PORT, () => {
console.log(`⚡️Server is running on port: ${PORT}`)
})