-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (53 loc) · 1.76 KB
/
index.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
const express = require('express')
const mongoose = require('mongoose');
const { MONGO_USERNAME, MONGO_PASSWORD, MONGO_IP, MONGO_PORT, REDIS_IP, REDIS_PORT, REDIS_SESSION_SECRET } = require('./config');
const postRouter = require('./routes/postRoutes');
const authRouter = require('./routes/authRoutes');
const mongoURL = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_IP}:${MONGO_PORT}/?authSource=admin`
const redis = require('redis')
const session = require('express-session')
const cors = require('cors')
const { protect } = require('./middleware/authMiddleware')
let RedisStore = require('connect-redis')(session)
let redisClient = redis.createClient({
host: REDIS_IP,
port: REDIS_PORT
})
const retryConnection = () => {
mongoose.connect(mongoURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: true,
})
.then(() => console.log('Connected successfull to database'))
.catch((e) => {
console.log("Failed to connect to mongo",e)
setTimeout(retryConnection, 5000)
})
}
retryConnection();
const app = express();
app.enable("trust proxy");
app.use(cors())
app.use(express.json());
app.use(
session({
store: new RedisStore({ client: redisClient }),
secret: REDIS_SESSION_SECRET,
cookie: {
secure: false,
resave: false,
saveUninitialized: false,
httpOnly: true,
maxAge: 60000
}
})
)
app.get('/api', (req, res) => {
console.log("yes it works")
return res.json({ "status": "This is also success message good" })
})
app.use('/api/posts', protect, postRouter)
app.use('/api/users', authRouter);
const port = process.env.PORT || 3000
app.listen(port, () => console.log(`listening on port ${port}`))