-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
121 lines (101 loc) · 3.19 KB
/
index.js
File metadata and controls
121 lines (101 loc) · 3.19 KB
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
import express from "express";
import logger from "./src/utils/logger.js";
import { metricsMiddleware, metricsRouter } from "./src/metrics.js";
import cors from "cors";
import productsRouter from "./src/routes/products.routes.js";
import routerUser from "./src/routes/auth.route.js";
import {authenticateToken} from "./src/middleware/authentication.js";
const app = express();
const PORT = process.env.PORT || 3000;
const ALLOWED_ORIGINS = process.env.ALLOWED_ORIGINS || "*";
const corsOptions = {
origin: ALLOWED_ORIGINS,
optionsSuccessStatus: 204,
methods: ["GET,HEAD,PUT,PATCH,POST,DELETE"],
alloweHeaders: ["Content-Type", "Authorization"],
exposedHeaders: ["Content-Length", "X-Kuma-Revision"],
credentials: true,
maxAge: 600,
};
// CORS middleware
app.use(cors(corsOptions));
app.use(express.json());
// Apply metrics middleware
app.use(metricsMiddleware);
// Authentication routes (no token required)
app.use("/api", routerUser);
// Expose /metrics for Prometheus to scrape
app.use("/metrics", metricsRouter);
// Global middleware
app.use((req, res, next) => {
console.log(`Datos received at: ${req.method} ${req.url}`);
res.header("Access-Control-Allow-Credentials", "true");
next();
});
// Add this route before the 404 handler
app.get("/", (req, res) => {
res.send("<h1>Welcome to the API</h1><p>Available endpoints: /HTML, /ping, /api, /health</p>");
});
// Main route
app.get("/HTML", (req, res) => {
res.send("<h1>Hello, World from express with middlewares!</h1>");
});
app.get("/ping", (req, res) => {
res.send("Answering /pong with express!");
logger.info("Answering /pong with express!");
});
// Protected routes (token required)
app.use(authenticateToken);
app.use("/api", productsRouter);
app.get("/items", (req, res) => {
const category = req.query.category;
const price = req.query.price;
res.send(`Categoría: ${category}, Precio: ${price}`);
logger.info(`Categoría: ${category}, Precio: ${price}`);
});
app.get("/health", (req, res) => {
const healthStatus = {
uptime: process.uptime(),
message: "OK",
timestamp: Date.now(),
};
try {
res.status(200).json(healthStatus);
logger.info("Health check successful");
} catch (error) {
healthStatus.message = error;
res.status(503).json(healthStatus);
logger.error(`Health check failed: ${error.message}`);
}
});
// 404 handler should be at the end
app.use((req, res) => {
logger.warn(`Route not found: ${req.method} ${req.originalUrl}`);
return res.status(404).json({
error: {
message: "Route not found"
}
});
});
// Centralized error handling middleware
app.use((error, req, res, next) => {
if (res.headersSent) {
return next(error);
}
logger.error(`Error encountered: ${error.message}`);
res.status(error.status || 500);
res.json({
error: {
message: error.message || "Internal Server Error"
}
});
});
// Start server for local development
if (process.env.NODE_ENV !== "production") {
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
logger.info(`Server running at http://localhost:${PORT}/`);
});
}
// Export app for Vercel serverless functions
export default app;