-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (48 loc) · 1.68 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
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const contactRouter = require("./routes/contactRoutes");
const dotenv = require("dotenv");
const helmet = require("helmet");
const cors = require('cors');
const rateLimit = require("express-rate-limit")
const app = express();
dotenv.config({ path: "./.env" });
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());
app.use(helmet());
app.use(helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'", "https://www.google-analytics.com"],
scriptSrc: ["'self'"],
styleSrc: ["'self'", "https://use.fontawesome.com/releases/v5.8.1/css/all.css", "*.googleapis.com"],
imgSrc: ["'self'", "i.imgur.com"],
fontSrc: ["'self'", "*.fontawesome.com", "*.googleapis.com", "*.gstatic.com"],
scriptSrcElem: ["'self'", "https://cdnjs.cloudflare.com", "https://www.googletagmanager.com", "'sha256-4ceKW4p347yM6DNp88zCxwCrtE/ORQ65LMuT492Osl4='"]
}
}));
app.set('trust proxy', 1);
const contactLimiter = rateLimit({
windowMs: 60 * 60 * 1000,
max: 30
});
app.use(express.static(path.join(__dirname, "public")));
app.route("/").get((req, res) => {
console.log("going to the home page")
res.status(200).render("index.html");
});
app.use("/contact", contactLimiter, contactRouter);
app.route("/error").get((req, res) => {
res.sendFile("/error.html", {
root: path.join(__dirname, 'public')
});
});
app.route("*").get((req, res) => {
res.sendFile("/doesnotexist.html", {
root: path.join(__dirname, 'public')
});
});
app.listen(process.env.PORT || 3000, () => {
console.log(`Listening on port ${process.env.PORT || 3000}`);
});