-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
101 lines (81 loc) · 3.09 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
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
const { setupWebSocketServer } = require("./modules/websocket/websocketService");
const connectMongoDB = require("./config/mongodb");
const express = require("express");
const cors = require("cors");
const http = require("http");
const morgan = require("morgan");
const geoip = require("geoip-lite");
const countries = require("i18n-iso-countries");
require("dotenv").config();
const mainApp = express();
const server = http.createServer(mainApp);
let logArray = []; // Array para almacenar los logs
// Token personalizado para la URL completa
morgan.token("full-url", (req) => {
const host = req.headers.host;
const url = req.originalUrl;
return `${req.protocol}:https://${host}${url}`;
});
// Token personalizado para la IP del cliente
morgan.token("client-ip", (req) => {
// return req.headers["x-forwarded-for"] || req.ip;
return req.ip;
});
// Token personalizado para la fecha y hora
morgan.token("date", () => new Date().toISOString().replace(/-/g, "").replace(/T/g, "-").slice(0, 14));
// Token personalizado para el país de origen (nombre completo)
morgan.token("country", (req) => {
const ip = req.headers["x-forwarded-for"] || req.ip;
const geo = geoip.lookup(ip);
return geo && geo.country ? countries.getName(geo.country, "en") : "Unknown";
});
// Middleware para almacenar los logs en el array
const customMorgan = morgan(":date :method :full-url :status :res[content] - :response-time ms :country :client-ip", {
stream: {
write: (log) => {
logArray.push(log.trim()); // Agregar el log al array
},
},
});
mainApp.use(customMorgan);
// mainApp.use(morgan("dev"));
mainApp.use(cors());
mainApp.use(express.json());
mainApp.use(express.urlencoded({ extended: true }));
const startServer = async () => {
try {
await connectMongoDB();
server.listen(3000, () => {
console.log(`listening on https://localhost:3000/`);
});
} catch (error) {
console.error("Error starting the server:", error);
}
};
startServer();
// Configurar WebSocket
setupWebSocketServer(server);
const apiRouter = express.Router();
apiRouter.use("/note", require("./api/note/"));
apiRouter.use("/auth", require("./api/auth/router"));
mainApp.use("/api", apiRouter);
mainApp.use("/contact", require("./modules/contact/app.js"));
mainApp.use("/img", require("./modules/img/app.js"));
mainApp.use("/feed", require("./modules/feed/app.js"));
mainApp.use("/chat", require("./modules/websocket/chatRoutes"));
mainApp.use("/stat", require("./modules/stat/app.js"));
mainApp.use("/notepad", require("./modules/notepad/app.js"));
mainApp.use("/", require("./api/shortener/shortener.router.js"));
mainApp.use("/", express.static("public"));
// // Endpoint para mostrar los logs
// mainApp.get("/logs", (req, res) => {
// res.json(logArray); // Devuelve los logs almacenados
// });
mainApp.get("/logs", (req, res) => {
const sortedLogs = logArray.slice().reverse(); // Copia el array y lo ordena en orden descendente
res.json(sortedLogs); // Devuelve los logs ordenados
});
// Catch-all route for 404 (Not Found) errors
mainApp.use((req, res, next) => {
res.status(404).sendFile("404.html", { root: "public" });
});