-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.js
More file actions
56 lines (47 loc) · 1.41 KB
/
Copy pathrouter.js
File metadata and controls
56 lines (47 loc) · 1.41 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
import { getHealth, getTime, getEstates } from "./handlers/apiHandlers.js";
import {
getHome,
getNewContact,
postContact,
} from "./handlers/viewHandlers.js";
import { staticHandler } from "./handlers/staticHandler.js";
import { sendHtmlError, sendJsonError } from "./utils/response.js";
export async function router(req, res) {
const pathname = req.url;
const method = req.method;
console.log(pathname);
try {
// Rutas de API
if (pathname === "/api/health") {
return await getHealth(req, res);
}
if (pathname === "/api/time") {
return await getTime(req, res);
}
if (pathname === "/api/estates") {
return await getEstates(req, res);
}
// Rutas de Vistas
if (pathname === "/contact" && method === "GET") {
return await getNewContact(req, res);
}
if (pathname === "/contact" && method === "POST") {
return await postContact(req, res);
}
if (pathname === "/") {
return await getHome(req, res);
}
// Fallback: Archivos Estáticos
return await staticHandler(req, res, pathname);
} catch (error) {
console.error("Router Error:", error);
const status = error.status || 500;
const message =
status === 500 ? "Error Interno del Servidor" : error.message;
if (pathname.startsWith("/api")) {
sendJsonError(res, message, status);
} else {
sendHtmlError(res, message, status);
}
}
}