-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
43 lines (38 loc) · 1.2 KB
/
server.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
// Configuration du serveur
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const userRoutes = require('./routes/user.routes');
const postRoutes = require('./routes/post.routes');
require('dotenv').config({ path: './config/.env' });
require('./config/db');
const { checkUser, requireAuth } = require('./middleware/auth.middleware');
const app = express();
const cors = require('cors');
const corsOptions = {
origin: process.env.CLIENT_URL,
credentials: true,
allowedHeaders: ['sessionId', 'Content-Type'],
exposeHeaders: ['sessionId'],
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false
};
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(cors(corsOptions));
// JWT
app.get('*', checkUser);
app.get('/jwtid', requireAuth, (req, res) => {
if (res.locals.user) {
res.status(200).send(res.locals.user._id);
}
});
// ROUTES
app.use('/api/users', userRoutes);
app.use('/api/posts', postRoutes);
// SERVER
// Utilisation de la variable d'environnement PORT
app.listen(process.env.PORT, () => {
console.log(`listening on port ${process.env.PORT}`);
});