-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
45 lines (38 loc) · 1.38 KB
/
app.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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const swaggerUi = require("swagger-ui-express");
const cors = require("cors");
const categoryRoute = require("./routes/cateRoute");
const productRoute = require("./routes/prodRoute");
const cartRoute = require("./routes/cartRoute");
const adminRoute = require("./routes/adminRoute");
const userRoute = require("./routes/userRoute");
const swaggerDocument = require("./swagger.json");
// Use body parser middleware to parse body of incoming requests
app.use(express.static('./public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());
// Routes which should handle requests
app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); //still working on it
app.use("/product", productRoute);
app.use("/category", categoryRoute);
app.use("/cart", cartRoute);
app.use("/admin", adminRoute);
app.use("/user", userRoute);
// Handle Error Requests
app.get("/", (req, res) => {
res.send('API IS NOW WORKING, append "/docs" to the current url to access API documentation');
});
// Handle Error Requests
app.use((req, res, next) => {
const error = new Error();
error.message = "Not Found";
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500).json({ error: error });
});
module.exports = app;