-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
74 lines (72 loc) · 1.77 KB
/
app.js
File metadata and controls
74 lines (72 loc) · 1.77 KB
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
import express from "express";
import dotenv from "dotenv";
import morgan from "morgan";
import helmet from "helmet";
import mongoSanitize from "express-mongo-sanitize";
import cookieParser from "cookie-parser";
import compression from "compression";
import fileUpload from "express-fileupload";
import cors from "cors";
import createHttpError from "http-errors";
import routes from "./routes/index.js";
//load dotenv config
dotenv.config();
// create Express App
const app = express();
//! Middlewares
//morgan
if (process.env.NODE_ENV !== "production") {
app.use(morgan("dev"));
}
//helmet
app.use(helmet());
//parse json request url
app.use(express.json());
// parse json request body(handle Form Submit /w-www-form)
app.use(express.urlencoded({ extended: true }));
//sanitize request Data prevent noSql injection
app.use(mongoSanitize());
//enable cookie parser read cookies from client
app.use(cookieParser());
//gzip compression
app.use(compression());
//file upload
app.use(
fileUpload({
useTempFiles: true,
tempFileDir: "/tmp/",
})
);
//cors
app.use(
cors({
origin: "http://localhost:5173",
credentials: true,
})
);
// app.get("/test", (req, res) => {
// throw createHttpError.BadRequest("This is a test error");
// });
//! Mount Routes
//? api v1 routes
app.use("/api/v1", routes);
app.use(express.static("public"));
app.use(async (req, res, next) => {
try {
// Render the index.html file from the public folder
res.sendFile("index.html", { root: "public" });
} catch (error) {
next(error); // Pass errors to the error handling middleware
}
});
//error handling
app.use(async (err, req, res, next) => {
res.status(err.status || 500);
res.send({
error: {
status: err.status || 500,
message: err.message,
},
});
});
export default app;