-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
36 lines (32 loc) · 1.17 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
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const authRoutes = require('./routes/user')
const booksRoutes = require('./routes/book')
const MongoDBURI = require('./.env/MongoDBURI')
const app = express();
//commentaire
//connexion à mongoDB
mongoose.connect(MongoDBURI,
{ useNewUrlParser: true,
useUnifiedTopology: true })
.then(() => console.log('Connexion à MongoDB réussie !'))
.catch(() => console.log('Connexion à MongoDB échouée !'));
//Accès au body des requetes
app.use(express.json());
//Résoud le CORS policy
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();
});
//Ajout des routes
app.use('/api/auth',authRoutes)
app.use('/api/books',booksRoutes)
//Gestion des images
app.use('/images', express.static(path.join(__dirname,'images')))
app.use('/',(req,res,next) => {
res.status(201).json({message : 'ça marche !'})
})
module.exports = app;