-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
54 lines (44 loc) · 1.26 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
import express from "express";
import { dbConnection } from "./DBConnection/DBConnect.js";
import errorHandler from "./utils/errorHandler.js";
import dotenv from "dotenv";
import cors from "cors";
import startup from "./startup/routes.js";
import globalErrorHandler from "./controllers/errorHandlerController.js";
import cookieParser from "cookie-parser";
const app = express();
app.use(cors());
app.use(express.json());
app.use(cookieParser());
dotenv.config();
dbConnection();
startup(app);
const port = process.env.PORT || 8000;
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content, Accept, Content-Type, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, PATCH, OPTIONS"
);
res.setHeader("Access-Control-Allow-Credentials", "true");
next();
});
app.all("*", (req, res, next) => {
next(new errorHandler(`Can't find ${req.originalUrl} on this server`, 404));
});
app.use(
cors({
credentials: true,
origin: true,
allowedHeaders: "*",
})
);
app.options("*", cors());
app.use(globalErrorHandler);
app.listen(port, () => {
console.log(`Server is running at http:localhost:${port}`);
});