-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
41 lines (34 loc) · 1.53 KB
/
api.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
const dotenv = require('dotenv').config()
const express = require('express')
const helmet = require('helmet')
const mongoose = require('mongoose')
const crypto = require('crypto')
const authRouter = require('./routers/auth.js').router
const adminRouter = require('./routers/admin.js').router
const voteRouter = require('./routers/vote.js').router
const authAdmin = require('./middlewares/authAdmin.js')
const app = express()
const {DB_USER, DB_NAME, DB_PASSWORD} = process.env
global.JWT_SECRET = crypto.randomBytes(50).toString('hex')
console.log(JWT_SECRET);
mongoose.set('useCreateIndex', true)
mongoose.set('useFindAndModify', false)
mongoose.connect(
`mongodb+srv://${DB_USER}:${DB_PASSWORD}@cluster0.mhwig.mongodb.net/${DB_NAME}?retryWrites=true&w=majority`,
{ useNewUrlParser: true, useUnifiedTopology: true }
)
const db = mongoose.connection
db.on('error', console.error.bind(console, 'ERROR: CANNOT CONNECT TO MONGO-DB'))
db.once('open', () => console.log('CONNECTED TO MONGO-DB'))
app.use(helmet())
app.use(express.json())
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
next();
});
app.use('/auth/', authRouter) //Route auth
app.use('/admin/', authAdmin, adminRouter) //Route admin
app.use('/vote/', voteRouter) //Route vote
app.listen(3000, () => {console.log('Serveur lancé sur le port 3000, bon développement !')})