-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
132 lines (123 loc) · 4.6 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const express = require("express");
const multer = require("multer");
const upload = multer({ dest: "upload/" });
const app = express();
const port = 8080;
const cors = require("cors");
const requestIp = require("request-ip");
const IMAGES = "/upload";
const GIF = "/gif";
const authenticateToken = require("./authentication/authenticateToken");
const authenticateTokenForUpload = require("./authentication/authenticateTokenForUpload");
const parseTimestamp = require("./functions/parseTimestamp");
const User = require("./database/schemas/userSchema");
const runAtSpecificTimeOfDay = require("./functions/runAtSpecificTimeOfDay");
const addLastDayToDates = require("./functions/addLastDayToDates");
const connectDatabase = require("./database/database");
const home = require("./routes/home");
const login = require("./routes/login");
const logout = require("./routes/logout");
const admin = require("./routes/admin");
const edit = require("./routes/edit");
const auctionEditor = require("./routes/auctionEditor");
const usersData = require("./routes/usersData");
const dates = require("./routes/dates");
const auctions = require("./routes/auctions");
const image = require("./routes/image");
const latestAuction = require("./routes/latestAuction");
const favicon = require("./routes/favicon");
const editAuction = require("./routes/editAuction");
const analitics = require("./routes/analitics");
const uploadImages = require("./routes/uploadImages");
const fastPriceEditor = require("./routes/fastPriceEditor");
const mostPopularKeywords = require("./routes/mostPopularKeywords");
const Dates = require("./database/schemas/dateSchema");
const viewCounter = require("./middleware/viewCounter");
require("dotenv").config();
app.use(cors());
app.options("*", cors());
app.set("view engine", "ejs");
app.use(express.json());
app.use("/public", express.static("public"));
app.use(requestIp.mw());
app.use(function (req, res, next) {
const ip = req.clientIp;
next();
});
app.locals.parseTimestamp = parseTimestamp;
process.on("uncaughtException", (error, origin) => {
console.log("----- Uncaught exception -----");
console.log(error);
console.log("----- Exception origin -----");
console.log(origin);
});
process.on("unhandledRejection", (reason, promise) => {
console.log("----- Unhandled Rejection at -----");
console.log(promise);
console.log("----- Reason -----");
console.log(reason);
});
// Database
connectDatabase();
// Run only on first startup to update databse records
// initialCreateUsers();
// Use this initially to insert all dates data. After first server startup!!!
//##################################################
// User.find(async (err, data) => {
// const dates = createGraphData(data);
// insertMultipleData(dates);
// console.log(dates);
// });
//##################################################
runAtSpecificTimeOfDay(0, 10, () => {
User.find(async (err, data) => {
if (err) return console.error(err);
addLastDayToDates(data);
});
});
//Multer configuration
const storage = multer.diskStorage({
destination: (req, file, cb) => {
if (file.fieldname === "image") {
cb(null, IMAGES);
} else if (file.fieldname === "gif") {
cb(null, GIF);
}
},
filename: (req, file, cb) => {
if (file.fieldname === "image") {
const fileName = req.body.title;
cb(null, fileName);
} else if (file.fieldname === "gif") {
cb(null, "giffy" + Date.now());
}
},
});
const cpUpload = upload.fields([
{ name: "image", maxCount: 10 },
{ name: "gif", maxCount: 1 },
]);
// API Routes
app.get("/", home);
app.post("/login", login);
app.get("/logout", logout);
app.get("/admin", authenticateToken, admin);
app.get("/edit", authenticateToken, edit);
app.get("/edit/editor", authenticateToken, auctionEditor);
app.get("/users-data", authenticateToken, usersData);
app.get("/dates", authenticateToken, dates);
app.get("/api/auctions", auctions);
app.get("/api/latest", latestAuction);
app.get("/images/:key", image);
app.get("/favicon.ico", favicon);
app.post("/api/edit-price", authenticateToken, fastPriceEditor);
app.post("/api/edit", authenticateToken, editAuction);
app.post("/analitics", viewCounter, analitics);
app.post("/upload", authenticateTokenForUpload, cpUpload, uploadImages);
app.get("/api/most-popular-keywords", authenticateToken, mostPopularKeywords);
app.get("*", async (req, res) => {
res.status(404).json({ error: "Podana strona nie istnieje." });
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});