-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
72 lines (57 loc) · 1.86 KB
/
Copy pathapp.js
File metadata and controls
72 lines (57 loc) · 1.86 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
import express from "express";
import morgan from "morgan";
import cors from "cors";
import bodyParser from "body-parser";
import studentsRouter from "./routes/studentRoutes.js";
import teachersRouter from "./routes/teacherRoutes.js";
import adminRouter from "./routes/adminRouter.js";
import authRouter from "./routes/authRouter.js";
import path, { dirname } from "path";
import { fileURLToPath } from "url";
import AppError from "./uitils/appError.js";
import errorHandler from "./controllers/errorHandler.js";
import expressRateLimiter from "express-rate-limit";
import helmet from "helmet";
//import { xss } from "express-xss-sanitizer";
import ExpressMongoSanitize from "express-mongo-sanitize";
const __dirname = dirname(dirname(fileURLToPath(import.meta.url)));
const app = express();
//helmet is a security middlware
app.use(helmet())
//limit amount of login
// const limiter = expressRateLimiter({
// max: 5,
// windowMs: 5 * 60 * 1000,
// message: "too many request try again after 5mins"
// })
// app.use("/api" , limiter)
app.use((req, res, next) => {
console.log("Hello from Middleware");
// console.log(req.headers);
next();
});
app.use(cors());
app.use(morgan("dev"));
app.use(express.static(path.join(__dirname + "/public")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
//sanitaise data to help protect your site no sql querry injection
app.use(ExpressMongoSanitize())
//data sanitaise xss
//app.use(xss())
app.get("/", (req, res) => {
res.status(200).json({
status: "ok",
message: "Hello",
});
});
app.use("/api/v1/students", studentsRouter);
app.use("/api/v1/teachers", teachersRouter);
app.use("/api/v1/auth", authRouter);
app.use("/api/v1/auth", adminRouter)
//handle 404 error
app.get("*", (req, res, next) => {
next(new AppError(`cannot find ${req.originalUrl} on the server`));
});
app.use(errorHandler);
export default app;