-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (63 loc) · 1.83 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
const express = require("express");
const mongoose = require("mongoose");
const sauceRoute = require("./routes/sauce");
const likeRoute = require("./routes/liked");
const userRoute = require("./routes/user");
const path = require("path");
const helmet = require("helmet");
const session = require("express-session");
const mongoSanitize = require("express-mongo-sanitize");
const app = express();
require("dotenv").config();
//requête vers mongodb
mongoose
.connect(process.env.MONGO_CONNECTION_SUPER_ADMIN, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("Connexion à MongoDB réussie !"))
.catch((error) => console.log("Connexion à MongoDB échouée !: " + error));
//Requête CORS
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"
);
next();
});
//En tête http sécurité
app.use(helmet());
//désactivation de l'en-tête X-Powered-By
app.disable("x-powered-by");
//coockie
app.set("trust proxy", 1); // trust first proxy
app.use(
session({
secret: "s3Cur3",
name: "sessionId",
})
);
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Pour supprimer des données, utilisez:
app.use(mongoSanitize());
// Ou, pour remplacer les caractères interdits par _, utilisez:
app.use(
mongoSanitize({
replaceWith: "_",
})
);
//Route vers le stockage des images
app.use("/images", express.static(path.join(__dirname, "images")));
//route utilisateur
app.use("/api/auth", userRoute);
//Routes sauces
app.use("/api/sauces", sauceRoute);
//Routes de likes
app.use("/api/sauces", likeRoute);
module.exports = app;