-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (54 loc) · 1.81 KB
/
server.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
import express from "express";
import cors from "cors";
import cookieParser from "cookie-parser";
import Route from "./routes/index.js";
import { PORT, WHITE_LISTED_END_POINTS } from "./config/env.config.js";
import helmet from "helmet";
import { connectToMongo } from "./utils/connectToMongo.util.js";
// === CREATE SERVER ===
const server = express();
// Allow request from any source. In real production, this should be limited to allowed origins only
const allowedOrigins =
WHITE_LISTED_END_POINTS.split(","); /** other domains if any */
// console.log(allowedOrigins);
server.use(
cors({
origin: function (origin, callback) {
// allow requests with no origin (like mobile apps or curl requests)
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1 || !origin) {
// Not allowed
var msg =
"The CORS policy for this site does not allow access from the specified Origin.";
return callback(new Error(msg), false);
}
return callback(null, true);
},
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', // Specify allowed HTTP methods
credentials: true // Allow cookies to be sent
})
);
server.disable("x-powered-by"); //Reduce fingerprinting
server.use(cookieParser());
server.use(express.urlencoded({ extended: false }));
server.use(express.json());
// Use helmet to set up various security headers
server.use(helmet());
// Set up Content Security Policy separately
server.use(
helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'"]
}
})
);
// === CREATE DATABASE ===
await connectToMongo();
// === CONFIGURE ROUTES ===
// Configure Route
Route(server);
// === 5 - START UP SERVER ===
server.listen(PORT, () =>
console.log(`Server running on http://localhost:${PORT}`)
);