-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
78 lines (64 loc) · 2.03 KB
/
server.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const express = require("express");
const path = require("path");
const dotenv = require("dotenv");
const colors = require("colors");
const cookieParser = require("cookie-parser");
const { Sequelize } = require("sequelize");
const cron = require("node-cron");
const { refreshMLToken } = require("./utils/mlAPI");
// utils
const {
recordAllProducts,
updateCatalogueProducts,
} = require("./utils/dailyRoutines");
// load env vars
dotenv.config({ path: "./config/config.env" });
const app = express();
// body parser
app.use(express.json());
// cookie parser
app.use(cookieParser());
// routes
const auth = require("./routes/auth");
const users = require("./routes/users");
const products = require("./routes/products");
const records = require("./routes/records");
// mount routes
app.use("/api/auth", auth);
app.use("/api/users", users);
app.use("/api/products", products);
app.use("/api/records", records);
// refreashs token when server is up
(async () => await refreshMLToken())();
(async () => await recordAllProducts())();
// scheduling token refreashing
cron.schedule("0 */5 * * *", () => {
let d = new Date();
d = new Date(d.valueOf() - d.getTimezoneOffset() * 60000);
console.log(`[${d.toGMTString()}] refreshing ML token...`);
refreshMLToken();
});
// scheduling record making
cron.schedule("0 */1 * * *", async () => {
let d = new Date();
d = new Date(d.valueOf() - d.getTimezoneOffset() * 60000);
console.log(`[${d.toGMTString()}] recording all products...`);
// await updateCatalogueProducts(); // before recording products
await recordAllProducts(); // each 24h
});
// serve static assets in product ion
if (process.env.NODE_ENV == "production") {
// set static folder
app.use(express.static("client/build"));
// create generic route
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
const PORT = process.env.PORT || 5000;
const server = app.listen(
PORT,
console.log(
`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`.yellow.bold
)
);