-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
92 lines (82 loc) · 2.65 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
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
import express from 'express'
import cors from 'cors'
import swaggerJSDoc from 'swagger-jsdoc'
import swaggerUI from 'swagger-ui-express'
import { ConnectDB } from './Config/SSEContext.js'
import { AdminRouter } from './Web/Routes/Api/AdminRouter.js'
import { CartRouter } from './Web/Routes/Api/CartRouter.js'
import { ChatRouter } from './Web/Routes/Api/ChatRouter.js'
import { ProductRouter } from './Web/Routes/Api/ProductRouter.js'
import { UserRouter } from './Web/Routes/Api/UserRouter.js'
import { WalletRouter } from './Web/Routes/Api/WalletRouter.js'
const app = express()
const router = express.Router()
var corsOptions = {
origin: 'http://localhost:3000',
optionsSuccessStatus: 200 //* some legacy browsers (IE11, various SmartTVs) choke on 204
}
// * Swagger Configuration
const options = {
definition: {
openapi: "3.0.0",
info: {
title: "OnlineShopping Server",
version: "0.3.0",
description: "APIs for online shoping"
},
servers: [
{
url: "http://192.168.10.10:6900"
},
{
url: "https://sseserver1.herokuapp.com"
},
{
url: "https://online-shopping-server.herokuapp.com"
},
{
url: "http://localhost:6900"
},
],
},
apis: ["./Web/Routes/Api/*.js"]
}
const specs = swaggerJSDoc(options);
// app.options('*', cors({ credentials: true, origin: true })) // include before other routes
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(specs));
// router.use('/api-docs', swaggerUI.serve);
// router.get('/api-docs', swaggerUI.setup(swaggerDocs));
// * Connect to DB
ConnectDB()
// * Init Middleware
app.use(cors()) //* only this working
app.use(express.json({ extended: false }))
app.get(
'/',
// cors(corsOptions),
(req, res) => {
var hostname = req.headers.host;
res.writeHead(302, {
'Location': `/api-docs`
//add other headers here...
});
res.end();
})
// * Define Routes
app.use('/Api/Cart', CartRouter)
app.use('/Api/Chat', ChatRouter)
app.use('/Api/Product', ProductRouter)
app.use('/Api/User', UserRouter)
app.use('/Api/Wallet', WalletRouter)
app.use('/Api/Admin', AdminRouter)
const PORT = process.env.PORT || 6900
app.listen(
PORT,
// `192.168.10.8`, //todo: home PTCL1
// `192.168.10.6`, //todo: home PTCL2
// `192.168.10.4`, //todo: home PTCL3
// `192.168.10.9`, //todo: home PTCL4
// `192.168.10.7`, //todo: home PTCL5
// `192.168.43.249`, //todo: home Zong4G
() => console.info(`Server is running on port ${PORT}`)
)