-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (97 loc) · 2.85 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const express = require("express")
const app = express()
// const bodyParser = require("body-parser")
require("dotenv").config()
const cors = require("cors")
require("./middleware/auth")
const response = require("./middleware/response")
const db = require("./config/database")
require("./config/relation")
// const flamelinkApp = require('./config/flamelinkSDK')
// const swaggerJsDoc = require('swagger-jsdoc')
// const swaggerUi = require('swagger-ui-express')
// const swaggerOptions = {
// swaggerDefinition: {
// info: {
// title: "SmartCanteen API",
// description: "Documentation of SmartCanteen Backend",
// contact: {
// name: "Admin"
// },
// servers: ["http://localhost:5000"]
// }
// },
// apis: ['./module/Admin/routes_admin.js']
// }
// const swaggerDocs = swaggerJsDoc(swaggerOptions)
// app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
// cors
let allowedOrigins = [
"http://localhost:5000",
"http://localhost:3000",
"http://localhost",
"https://smartcanteen-admin.netlify.app",
"https://smartcanteen-client.netlify.app",
]
app.use(
cors({
credentials: true,
origin(origin, callback) {
if (!origin) return callback(null, true)
if (allowedOrigins.indexOf(origin) === -1) {
let msg = "The CORS policy for this site does not "
+ "allow access from the specified Origin."
return callback(new Error(msg), false)
}
return callback(null, true)
},
}),
)
// app.use(
// cors({
// credentials: true,
// origin: true
// })
// )
// app.use(cors())
// middleware
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use(response)
// router
const adminRoute = require("./module/Admin/routes_admin")
const sellerRoute = require("./module/Seller/routes_seller")
const buyerRoute = require("./module/Buyer/routes_buyer")
const foodRoute = require("./module/Food/routes_food")
const tenantRoute = require("./module/Tenant/routes_tenant")
const orderRoute = require("./module/Order/routes_order")
app.get("/", (req, res) => {
res.send("Hello")
})
app.use("/api/admin", adminRoute)
app.use("/api/penjual", sellerRoute)
app.use("/api/pembeli", buyerRoute)
app.use("/api/makanan", foodRoute)
app.use("/api/warung", tenantRoute)
app.use("/api/order", orderRoute)
// error handling
app.use((req, res, next) => {
let err = new Error("Route not found")
err.status = 404
next(err)
})
app.use(async (err, req, res) => {
// deleteFoto(req)
const { message } = err
const status = err.status || 500
console.log(err)
res.sendError(null, message, status)
})
const port = process.env.PORT || 5000
app.listen(port, () => {
db.sync({})
// db.sync({ force:true })
.then(() => console.log(`app is running on port ${port}`))
.catch((err) => console.log(err.message))
console.log(`app is running on port ${port}`)
})