-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (47 loc) · 1.49 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
const express=require('express')
const mongoose=require('mongoose')
const {MONGO_IP,MONGO_PASSWORD,MONGO_PORT,MONGO_USER, REDIS_URL, REDIS_PORT, SESSION_SECRET}=require('./config/config')
const session=require('express-session')
const redis=require('redis')
let RedisStore= require('connect-redis')(session)
const cors=require('cors')
let redisClient=redis.createClient({
host: REDIS_URL,
port: REDIS_PORT
})
const postRouter=require('./routes/postRoutes')
const userRouter=require('./routes/userRoutes')
const app=express()
const mongoURL = `mongodb://${MONGO_USER}:${MONGO_PASSWORD}@${MONGO_IP}:${MONGO_PORT}/?authSource=admin`
const connectWithRetry= () => {
mongoose.connect(mongoURL,{
useNewUrlparser:true,
useUnifiedTopology:true,
})
.then(() => console.log('successfully connected to DB'))
.catch((e) => {console.log(e)
setTimeout(connectWithRetry,5000)})
}
connectWithRetry()
app.enable('trust proxy')
app.use(cors({}))
app.use(session({
store: new RedisStore({client:redisClient}),
secret: SESSION_SECRET,
cookie:{
secure:false,
resave:false,
saveUninitialized:false,
httpOnly:true,
maxAge:60000
}
}))
app.use(express.json())
app.get('/',(req,res)=>{
res.send('<h2>Hi there!!</h2>')
console.log("Yeah it ran")
});
app.use('/api/v1/posts',postRouter)
app.use('/api/v1/users',userRouter)
const port=process.env.PORT || 3000
app.listen(port,()=>console.log(`listening on port ${port}`))