-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (38 loc) · 1.5 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
// LES PACKAGES NPM INSTALLES //
const express = require('express');
const helmet = require("helmet");
const rateLimit = require("express-rate-limit");
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const path = require('path');
require('dotenv').config()
// ROUTES PERMETTENT D'ALLER CHERCHER LES INFORMATIONS DANS L'API
const saucesRoutes = require('./routes/sauces');
const userRoutes = require('./routes/user');
// UTILISER POUR LIMITER LA REPETITION DE REQUETE //
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100
});
// CONNEXION A MANGODB //
mongoose.connect(process.env.DB_URI,
{ useNewUrlParser: true,
useUnifiedTopology: true })
.then(() => console.log('Connexion à MongoDB réussie !'))
.catch(() => console.log('Connexion à MongoDB échouée !'));
const app = express();
app.use(helmet());
// AUTORISE
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();
});
// LES ROUTES POUR ACCEDER A L'API //
app.use(bodyParser.json());
app.use('/api/sauces', saucesRoutes);
app.use('/api/auth', userRoutes);
app.use(limiter);
app.use('/images', express.static(path.join(__dirname, 'images')));
module.exports = app;