-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
51 lines (43 loc) · 1.62 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
const express = require('express')
const cors = require('cors')
const path = require('path')
const cookieParser = require('cookie-parser')
require('dotenv').config()
const app = express()
const http = require('http').createServer(app)
// Express App Config
app.use(cookieParser())
app.use(express.json())
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.resolve(__dirname, 'public')))
} else {
const corsOptions = {
origin: ['http://127.0.0.1:5173', 'http://localhost:5173'],
credentials: true
}
app.use(cors(corsOptions))
}
const authRoutes = require('./api/auth/auth.routes')
const userRoutes = require('./api/user/user.routes')
const stayRoutes = require('./api/stay/stay.routes')
const orderRoutes = require('./api/order/order.routes')
const { setupSocketAPI } = require('./services/socket.service')
// Routes
const setupAsyncLocalStorage = require('./middlewares/setupAls.middleware')
app.all('*', setupAsyncLocalStorage)
app.use('/api/auth', authRoutes)
app.use('/api/user', userRoutes)
app.use('/api/stay', stayRoutes)
app.use('/api/order', orderRoutes)
setupSocketAPI(http)
// Make every server-side-route to match the index.html
// so when requesting http://localhost:3030/index.html/stay/123 it will still respond with
// our SPA (single page app) (the index.html file) and allow vue/react-router to take it from there
app.get('/**', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'))
})
const logger = require('./services/logger.service')
const port = process.env.PORT || 3030
http.listen(port, () => {
logger.info('Server is running on port: ' + port)
})