-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
32 lines (27 loc) · 1.12 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
const express = require('express');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const app = express();
const stuffRoutes = require('./routes/stuff');
const userRoutes = require('./routes/user');
dotenv.config();
mongoose.connect('mongodb+srv://' + process.env.DB_USER +':'+ process.env.DB_PASSWORD + '@cluster0.tnvqqls.mongodb.net/?retryWrites=true&w=majority',
{ useNewUrlParser: true,
useUnifiedTopology: true })
.then(() => console.log('Connexion à MongoDB réussie !'))
.catch(() => console.log('Connexion à MongoDB échouée !'));
// can extract request
app.use(express.json());
app.use('/api/stuff', stuffRoutes);
app.use('/api/auth', userRoutes);
// add CORS
app.use((req, res, next) => {
// Allow access to API
res.setHeader('Access-Control-Allow-Origin', '*');
// Allow some headers on request
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content, Accept, Content-Type, Authorization');
// Allow some methods
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
next();
});
module.exports = app;