-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
83 lines (67 loc) · 2.06 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Libray for Web service
const express = require('express');
const app = express();
const routes = require('./routes');
// Library for redis (session)
const session = require('express-session');
const redis = require('redis');
const connectRedis = require('connect-redis');
// Libray for Env
require('dotenv').config()
// Libray for Mongo database
const mongoose = require('mongoose');
// Libray for Secure API with cors
const cors = require('cors');
// Redis session
const RedisStore = connectRedis(session)
//Configure redis client
const redisClient = redis.createClient({
host: process.env.REDIS_URL_DOCKER,
port: process.env.REDIS_PORT
})
redisClient.on('error', (err) => {
console.log('Could not establish a connection with redis. ' + err);
});
redisClient.on('connect', () => {
console.log('Connected to redis successfully');
});
//Configure session middleware
app.use(session({
store: new RedisStore({ client: redisClient }),
secret: process.env.SECRET_SESSION,
resave: false,
saveUninitialized: false,
cookie: {
secure: false, // if true only transmit cookie over https
httpOnly: false, // if true prevent client side JS from reading the cookie
maxAge: Number(process.env.TIME_SESSION) // session max age in miliseconds (86400000ms = 1 day)
}
}));
// Parser (JSON)
app.use(express.urlencoded({ extended: true }))
app.use(express.json());
app.use(routes);
// Add cors for the security
app.use(cors({
origin:[`http://localhost:${process.env.PORT}`],
methods:['GET','POST'],
credentials: true // enable set cookie
}));
// API Listens port (.ENV)
app.listen(process.env.PORT_API,(err)=>{
if (err)
throw err;
console.log(`API is waiting on localhost:${process.env.PORT_API}`);
});
// MongoDB options
const options = {
keepAlive: 1,
useUnifiedTopology: true,
useNewUrlParser: true,
};
// Connect to Mongodb with option define before
mongoose.connect(process.env.URL_MONGO, options ,(err)=> {
if (err)
throw err;
console.log('Connected to Mongodb successfully');
});