-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (67 loc) · 2.09 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
const fs = require("fs");
const path = require("path");
const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const errorHandler = require("./middleware/mdberrorHandler.js");
const errorResponse = require("./utils/errorResponse");
// Initalise Routes
const auth = require("./routes/authRoutes");
const user = require("./routes/userRoutes");
const student = require("./routes/studentRoutes");
const contact = require("./routes/contactRoutes");
const recipient = require("./routes/recipientRoutes");
const processing = require("./routes/processingRoutes");
const enquiry = require("./routes/enquiryRoutes");
// load Env Variable
require("dotenv").config({
path: "./config/config.env",
});
// Initialising Express
const app = express();
// Cookie Parser
app.use(cookieParser());
// Initialising Express to use JSON
app.use(
express.json({
extended: false,
})
);
// Dev Logging Middleware
if (process.env.NODE_ENV === "development") {
// Cors helps to deal with react for localhost at port 6000 without any problem
app.use(
cors({
origin: process.env.CLIENT_URL,
})
);
// Config only for development
app.use(morgan("dev"));
}
// Set Static Folder Path
app.use(express.static(path.join(__dirname, "public")));
// Enable CORS
// app.use(cors());
// Mount Routes
app.use("/api/v1/auth", auth);
app.use("/api/v1/users", user);
app.use("/api/v1/students", student);
app.use("/api/v1/contacts", contact);
app.use("/api/v1/recipient", recipient);
app.use("/api/v1/processing", processing);
app.use("/api/v1/enquiry", enquiry);
// Capture Errors for Routes that do not exist
app.all("*", (req, res, next) => {
next(new errorResponse(`Requested url ${req.originalUrl} not found`, 404));
});
// Middleware For routes that do not exist
app.use((req, res, next) => {
res.status(404).json({
succes: false,
message: "Page Not Found",
});
});
// @desc Error Handler middleware should always be below the routes to catch the errors and not cause code block
app.use(errorHandler);
module.exports = app;