-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (46 loc) · 1.55 KB
/
app.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 logger = require("morgan");
const helmet = require("helmet");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();
const URI = process.env.URI;
const connectWithRetry = (uris, options, maxAttempts = 5) => {
connectWithRetry.timeout = connectWithRetry.timeout || 0;
return mongoose.connect(uris, options, err => {
if (err)
if (connectWithRetry.timeout <= (maxAttempts - 1) * 5000) {
console.error(
`Failed to connect to mongo on startup - retrying in ${
(connectWithRetry.timeout += 5000) / 1000
} sec`,
connectWithRetry.previousError != "" + err
? `\n${(connectWithRetry.previousError = err)}`
: ""
);
setTimeout(connectWithRetry, connectWithRetry.timeout, uris, options);
} else process.exit(1);
else console.log("Connected to MongoDB successfully!");
});
};
connectWithRetry(URI, {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true
});
const app = express();
app.use(helmet());
app.use(cors());
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use("/api", require("./routes/api"));
app.use("/api", require("./routes/auth"));
app.use("/api/remove", require("./routes/remove"));
app.use("/api/info", require("./routes/info"));
app.use((err, req, res) => {
console.error(err);
res.status(422).send({ success: false, error: err.message });
});
module.exports = app;