-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (47 loc) · 1.68 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
const express = require("express");
const bodyParser = require("body-parser");
const morgan = require("morgan");
const cors = require("cors");
const mongoose = require("mongoose");
const cookieParser = require("cookie-parser");
const errorsManagerMiddleware = require("./server/src/middlewares/errorsManagerMiddleware");
require("dotenv").config();
const productRoute = require("./server/src/routes/productRoute");
const userRoute = require("./server/src/routes/userRoute");
const {downloadTaxonomiesFiles, loadTaxonomiesFiles} = require("./server/src/helpers/open food facts/taxonomies");
// Setup DB connection
mongoose
.connect(process.env.DB_CONNECTION, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => console.log("DB connected :)"))
.catch((error) => console.log(error));
downloadTaxonomiesFiles(false).then(() => {
loadTaxonomiesFiles();
});
if (process.env.DOWNLOAD_TAXONOMIES_REGULARLY === "TRUE") {
setInterval(() => {
downloadTaxonomiesFiles(true).then(() => {
loadTaxonomiesFiles();
});
}, 60 * 60 * 1000);
}
// Setup Express App
const App = express();
// Middlewares
App.use(morgan("short"));
App.use(cookieParser());
App.use(bodyParser.json());
const corsOrigin = process.env.ENVIRONMENT === "PRODUCTION" ? process.env.CLIENT_URL : true;
App.use(cors({credentials: true, origin: corsOrigin}));
// Routes
App.use("/api/products", productRoute);
App.use("/api/auth", userRoute);
// Errors manager middleware
App.use(errorsManagerMiddleware());
App.listen(process.env.PORT, () => {
console.log(`I'm running on ${process.env.PORT} :)`);
});