-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (62 loc) · 1.6 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
const express = require("express");
const session = require("express-session");
// Require new packages below:
const helmet = require("helmet");
// Add the 'check' function below:
const { check, validationResult } = require("express-validator");
const PORT = process.env.PORT || 4001;
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));
app.set("view engine", "ejs");
app.set('trust proxy', 1)
// Add helmet below:
app.use(helmet());
app.use(
session({
secret: "my-secret",
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 300000000,
sameSite: 'none',
// Add the appropiate properties below:
httpOnly: true,
secure: true,
},
})
);
app.get("/", (req, res) => {
res.render("home");
});
// Endpoint in development
app.post(
"/review",
[
// Add the middleware to validate email and restaurant info below:
check('email').isEmail(),
check('restaurant').notEmpty().blacklist("<>"),
check('rating').isNumeric(),
check('review').notEmpty().blacklist("<>")
],
(req, res) => {
var errors = validationResult(req).array();
console.log(`Errors found: ${JSON.stringify(errors)}`);
if (errors.length >= 1) {
res.redirect("/error");
} else {
console.log("Data was valid!");
res.redirect("/success");
}
}
);
app.get("/success", (req, res) => {
res.render("success");
});
app.get("/error", (req, res) => {
res.render("error");
});
app.listen(PORT, () =>
console.log(`The server is listening at port: http://localhost:${PORT}`)
);