-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
82 lines (70 loc) · 2.07 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
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
const express = require("express");
const cors = require("cors");
const { config } = require("./config");
// Initiliseing server application with express
const app = express();
app.use(
cors({
origin: [config.PROD_URL, config.LOCAL_URL],
credentials: true,
})
);
// middleware for getting data from body in json format
app.use(express.json());
// middleware for getting data as a search queries
app.use(express.urlencoded({ extended: true }));
// mongoose connection
require("./config/db").connect();
// for reading cookies and handling
const cookieParser = require("cookie-parser");
app.use(cookieParser());
// handaling file uploading
const fileUpload = require("express-fileupload");
app.use(
fileUpload({
useTempFiles: true,
tempFileDir: "tmp",
})
);
// morgan middleware to check response time when we hit an api route
const morgan = require("morgan");
morgan(":method :url :status :res[content-length] - :response-time ms");
app.use(require("morgan")("tiny"));
//swagger api docs
const Yaml = require("yamljs");
const swaggerUi = require("swagger-ui-express");
const swaggerDocument = Yaml.load("./swagger.yaml");
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
//import all routes
const home = require("./routes/home");
const user = require("./routes/user");
const product = require("./routes/product");
const payment = require("./routes/payment");
const order = require("./routes/order");
app.get("/ping", (req, res) => {
res.status(200).json({
message: "pong...",
});
});
// router middleware
app.use("/api/v1/", home);
app.use("/api/v1/", user);
app.use("/api/v1/", product);
app.use("/api/v1/", payment);
app.use("/api/v1/", order);
// removeing a directory
const fse = require("fs-extra");
setInterval(() => {
fse.pathExists("./tmp", (err, exists) => {
if (exists) {
setTimeout(() => {
fse
.remove("./tmp")
.then(() => console.log("Done"))
.catch((e) => console.log(e));
}, Date.now() + 1 * 60 * 60 * 1000);
}
});
}, 1000 * 10);
// exporting the app
module.exports = app;