-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
65 lines (57 loc) · 1.76 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
require("dotenv").config()
require("express-async-errors")
const express = require("express")
const app = express()
// Rest of the packages
const morgan = require("morgan") //HTTP request logger middleware
const cookieParser = require("cookie-parser")
const fileUpload = require("express-fileupload")
// Require Database
const connectDB = require("./db/connect")
// Require Routers
const authRouter = require("./routes/authRoutes")
const userRouter = require("./routes/userRoutes")
const productRouter = require("./routes/productRoutes")
const reviewRouter = require("./routes/reviewRoutes")
const orderRouter = require("./routes/orderRoutes")
// Require Middleware
const notFoundMiddleware = require("./middleware/not-found")
const errorHandlerMiddleware = require("./middleware/error-handler")
// Invoke Extra packages
app.use(morgan("tiny"))
app.use(express.json())
app.use(cookieParser(process.env.JWT_SECRET))
app.use(express.static("./public"))
app.use(fileUpload())
// Home get
app.get("/", (req, res) => {
res.send("<h1> E-Commerce API</h1>")
})
// Testing route
app.get("/api/v1/", (req, res) => {
// console.log(req.cookies)
// console.log(req.signedCookies)
res.send("E-commerce API")
})
// Invoke Routers
app.use("/api/v1/auth", authRouter)
app.use("/api/v1/users", userRouter)
app.use("/api/v1/products", productRouter)
app.use("/api/v1/reviews", reviewRouter)
app.use("/api/v1/orders", orderRouter)
// Invoke Middleware
app.use(notFoundMiddleware)
app.use(errorHandlerMiddleware)
const port = process.env.PORT || 5000
const start = async () => {
try {
// Connect database
await connectDB(process.env.MONGO_URL)
app.listen(port, () =>
console.log(`🚀 Server is listening on port ${port}...`)
)
} catch (error) {
console.log(error)
}
}
start()