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