-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (49 loc) · 1.54 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
const express = require("express");
const mongoose = require("mongoose");
const { MONGO_USER, MONGO_PASSWORD, MONGO_IP, MONGO_PORT,REDIS_PORT, REDIS_URL, SESSION_SECRET } = require("./config/config");
const app = express()
const cors = require("cors");
const session = require("express-session");
const redis = require("redis");
let RedisStore = require("connect-redis")(session);
let redisClient = redis.createClient({
legacyMode: true,
socket: {
port: REDIS_PORT,
host: REDIS_URL
}
})
redisClient.connect().catch(console.error)
const postRouter = require("./routes/postRoutes");
const userRouter = require("./routes/userRoutes");
const connectWithRetry = () => {
mongoose.connect(`mongodb://${MONGO_USER}:${MONGO_PASSWORD}@${MONGO_IP}:${MONGO_PORT}/?authSource=admin`).then(() => {
console.log("Succesfully connected to Database");
}).catch((err) => {
console.log("Not Connected to Database ERROR! ", err);
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: 3000000,
}
}))
app.use(express.json());
app.get("/api/v1", (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}`'))