jerkjs 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -2
- package/README.md +1 -1
- package/examples.arj +0 -0
- package/index.js +1 -1
- package/lib/middleware/compressor.js +34 -18
- package/package.json +1 -1
- package/standard/CompressionTestController.js +38 -0
- package/standard/HealthController.js +16 -0
- package/standard/HomeController.js +12 -0
- package/standard/ProductController.js +18 -0
- package/standard/README.md +47 -0
- package/standard/UserController.js +23 -0
- package/standard/package.json +22 -0
- package/standard/routes.json +58 -0
- package/standard/server.js +140 -0
- package/cookies.txt +0 -5
- package/examples/advanced/controllers/productController.js +0 -64
- package/examples/advanced/controllers/userController.js +0 -85
- package/examples/advanced/routes.json +0 -51
- package/examples/advanced_example.js +0 -93
- package/examples/basic/controllers/userController.js +0 -85
- package/examples/basic_example.js +0 -72
- package/examples/frontend/README.md +0 -71
- package/examples/frontend/app.js +0 -71
- package/examples/frontend/controllers/apiController.js +0 -39
- package/examples/frontend/controllers/authController.js +0 -220
- package/examples/frontend/controllers/formController.js +0 -47
- package/examples/frontend/controllers/messageController.js +0 -96
- package/examples/frontend/controllers/pageController.js +0 -178
- package/examples/frontend/controllers/staticController.js +0 -167
- package/examples/frontend/routes.json +0 -90
- package/examples/hooks/app.js +0 -136
- package/examples/hooks/controllers/authController.js +0 -54
- package/examples/hooks/controllers/mainController.js +0 -41
- package/examples/hooks/controllers/productController.js +0 -39
- package/examples/hooks/controllers/userController.js +0 -69
- package/examples/hooks/routes.json +0 -51
- package/examples/hooks/views/home.html +0 -50
- package/examples/mvc_example/app.js +0 -138
- package/examples/mvc_example/views/home/index.html +0 -26
- package/examples/mvc_example/views/home/simple.html +0 -3
- package/examples/mvc_example/views/layout.html +0 -23
- package/examples/mvc_example/views/test.html +0 -3
- package/examples/mvc_example/views/user/invalid.html +0 -6
- package/examples/mvc_example/views/user/list.html +0 -36
- package/examples/mvc_example/views/user/notfound.html +0 -6
- package/examples/mvc_example/views/user/profile.html +0 -11
- package/examples/mvc_routes_example/app.js +0 -34
- package/examples/mvc_routes_example/controllers/mainController.js +0 -27
- package/examples/mvc_routes_example/controllers/productController.js +0 -47
- package/examples/mvc_routes_example/controllers/userController.js +0 -76
- package/examples/mvc_routes_example/routes.json +0 -30
- package/examples/mvc_routes_example/views/layout.html +0 -31
- package/examples/mvc_routes_example/views/main/index.html +0 -11
- package/examples/mvc_routes_example/views/product/catalog.html +0 -24
- package/examples/mvc_routes_example/views/user/invalid.html +0 -6
- package/examples/mvc_routes_example/views/user/list.html +0 -40
- package/examples/mvc_routes_example/views/user/notfound.html +0 -6
- package/examples/mvc_routes_example/views/user/profile.html +0 -18
- package/examples/mvc_welcome/README.md +0 -34
- package/examples/mvc_welcome/app.js +0 -50
- package/examples/mvc_welcome/controllers/welcomeController.js +0 -41
- package/examples/mvc_welcome/package.json +0 -26
- package/examples/mvc_welcome/views/home/welcome.html +0 -82
- package/examples/performance_demo/app.js +0 -172
- package/examples/performance_demo_with_hooks/README.md +0 -52
- package/examples/performance_demo_with_hooks/app.js +0 -290
- package/examples/public/README.md +0 -92
- package/examples/public/app.js +0 -72
- package/examples/public/controllers/healthController.js +0 -20
- package/examples/public/controllers/mainController.js +0 -22
- package/examples/public/controllers/userController.js +0 -139
- package/examples/public/routes.json +0 -51
- package/examples/v2/README.md +0 -72
- package/examples/v2/app.js +0 -74
- package/examples/v2/app_fixed.js +0 -74
- package/examples/v2/controllers/authController.js +0 -64
- package/examples/v2/controllers/mainController.js +0 -24
- package/examples/v2/controllers/protectedController.js +0 -12
- package/examples/v2/controllers/userController.js +0 -16
- package/examples/v2/package.json +0 -27
- package/examples/v2/routes.json +0 -30
- package/examples/v2/test_api.sh +0 -47
- package/examples/v2/tokens_example.sqlite +0 -0
- package/examples/v2.1_firewall_demo/README.md +0 -113
- package/examples/v2.1_firewall_demo/app.js +0 -182
- package/examples/v2.1_firewall_demo/package.json +0 -27
- package/examples/v2.1_hooks_demo/README.md +0 -85
- package/examples/v2.1_hooks_demo/app.js +0 -101
- package/examples/v2.1_hooks_demo/controllers/hooksController.js +0 -29
- package/examples/v2.1_hooks_demo/controllers/mainController.js +0 -18
- package/examples/v2.1_hooks_demo/package.json +0 -27
- package/examples/v2.1_hooks_demo/routes.json +0 -16
- package/examples/v2.1_openapi_demo/README.md +0 -82
- package/examples/v2.1_openapi_demo/app.js +0 -296
- package/examples/v2.1_openapi_demo/package.json +0 -26
- package/examples/v2_cors/README.md +0 -82
- package/examples/v2_cors/app.js +0 -108
- package/examples/v2_cors/package.json +0 -23
- package/examples/v2_json_auth/README.md +0 -83
- package/examples/v2_json_auth/app.js +0 -72
- package/examples/v2_json_auth/controllers/authController.js +0 -67
- package/examples/v2_json_auth/controllers/mainController.js +0 -16
- package/examples/v2_json_auth/controllers/protectedController.js +0 -12
- package/examples/v2_json_auth/controllers/tokenController.js +0 -28
- package/examples/v2_json_auth/controllers/userController.js +0 -15
- package/examples/v2_json_auth/package.json +0 -26
- package/examples/v2_json_auth/routes.json +0 -37
- package/examples/v2_json_auth/tokens.json +0 -20
- package/examples/v2_mariadb_auth/README.md +0 -94
- package/examples/v2_mariadb_auth/app.js +0 -81
- package/examples/v2_mariadb_auth/controllers/authController.js +0 -95
- package/examples/v2_mariadb_auth/controllers/mainController.js +0 -31
- package/examples/v2_mariadb_auth/controllers/protectedController.js +0 -12
- package/examples/v2_mariadb_auth/controllers/userController.js +0 -17
- package/examples/v2_mariadb_auth/package.json +0 -27
- package/examples/v2_mariadb_auth/routes.json +0 -37
- package/examples/v2_no_auth/README.md +0 -75
- package/examples/v2_no_auth/app.js +0 -72
- package/examples/v2_no_auth/controllers/healthController.js +0 -14
- package/examples/v2_no_auth/controllers/mainController.js +0 -19
- package/examples/v2_no_auth/controllers/productController.js +0 -31
- package/examples/v2_no_auth/controllers/publicController.js +0 -16
- package/examples/v2_no_auth/package.json +0 -22
- package/examples/v2_no_auth/routes.json +0 -37
- package/examples/v2_oauth/README.md +0 -70
- package/examples/v2_oauth/app.js +0 -90
- package/examples/v2_oauth/controllers/mainController.js +0 -45
- package/examples/v2_oauth/controllers/oauthController.js +0 -247
- package/examples/v2_oauth/controllers/protectedController.js +0 -13
- package/examples/v2_oauth/controllers/userController.js +0 -17
- package/examples/v2_oauth/package.json +0 -26
- package/examples/v2_oauth/routes.json +0 -44
- package/examples/v2_openapi/README.md +0 -77
- package/examples/v2_openapi/app.js +0 -222
- package/examples/v2_openapi/controllers/authController.js +0 -52
- package/examples/v2_openapi/controllers/mainController.js +0 -26
- package/examples/v2_openapi/controllers/productController.js +0 -17
- package/examples/v2_openapi/controllers/userController.js +0 -27
- package/examples/v2_openapi/package.json +0 -26
- package/examples/v2_openapi/routes.json +0 -37
- package/v2examplle/v2_json_auth/README.md +0 -83
- package/v2examplle/v2_json_auth/app.js +0 -72
- package/v2examplle/v2_json_auth/controllers/authController.js +0 -67
- package/v2examplle/v2_json_auth/controllers/mainController.js +0 -16
- package/v2examplle/v2_json_auth/controllers/protectedController.js +0 -12
- package/v2examplle/v2_json_auth/controllers/tokenController.js +0 -28
- package/v2examplle/v2_json_auth/controllers/userController.js +0 -15
- package/v2examplle/v2_json_auth/package.json +0 -26
- package/v2examplle/v2_json_auth/routes.json +0 -37
- package/v2examplle/v2_json_auth/tokens.json +0 -20
- package/v2examplle/v2_mariadb_auth/README.md +0 -94
- package/v2examplle/v2_mariadb_auth/app.js +0 -81
- package/v2examplle/v2_mariadb_auth/controllers/authController.js +0 -95
- package/v2examplle/v2_mariadb_auth/controllers/mainController.js +0 -31
- package/v2examplle/v2_mariadb_auth/controllers/protectedController.js +0 -12
- package/v2examplle/v2_mariadb_auth/controllers/userController.js +0 -17
- package/v2examplle/v2_mariadb_auth/package.json +0 -27
- package/v2examplle/v2_mariadb_auth/routes.json +0 -37
- package/v2examplle/v2_sqlite_auth/README.md +0 -72
- package/v2examplle/v2_sqlite_auth/app.js +0 -74
- package/v2examplle/v2_sqlite_auth/app_fixed.js +0 -74
- package/v2examplle/v2_sqlite_auth/controllers/authController.js +0 -64
- package/v2examplle/v2_sqlite_auth/controllers/mainController.js +0 -24
- package/v2examplle/v2_sqlite_auth/controllers/protectedController.js +0 -12
- package/v2examplle/v2_sqlite_auth/controllers/userController.js +0 -16
- package/v2examplle/v2_sqlite_auth/package.json +0 -27
- package/v2examplle/v2_sqlite_auth/routes.json +0 -30
- package/v2examplle/v2_sqlite_auth/test_api.sh +0 -47
- package/v2examplle/v2_sqlite_auth/tokens_example.sqlite +0 -0
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
const staticController = {
|
|
2
|
-
getCSS: (req, res) => {
|
|
3
|
-
const css = `
|
|
4
|
-
body {
|
|
5
|
-
font-family: Arial, sans-serif;
|
|
6
|
-
margin: 0;
|
|
7
|
-
padding: 0;
|
|
8
|
-
background-color: #f5f5f5;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
.container {
|
|
12
|
-
max-width: 1200px;
|
|
13
|
-
margin: 0 auto;
|
|
14
|
-
padding: 20px;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
header {
|
|
18
|
-
background-color: #333;
|
|
19
|
-
color: white;
|
|
20
|
-
padding: 1rem;
|
|
21
|
-
border-radius: 5px;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
header h1 {
|
|
25
|
-
margin: 0;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
nav ul {
|
|
29
|
-
list-style-type: none;
|
|
30
|
-
padding: 0;
|
|
31
|
-
margin: 10px 0 0 0;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
nav ul li {
|
|
35
|
-
display: inline;
|
|
36
|
-
margin-right: 20px;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
nav ul li a {
|
|
40
|
-
color: white;
|
|
41
|
-
text-decoration: none;
|
|
42
|
-
padding: 5px 10px;
|
|
43
|
-
border-radius: 3px;
|
|
44
|
-
transition: background-color 0.3s;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
nav ul li a:hover {
|
|
48
|
-
background-color: #555;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
main {
|
|
52
|
-
margin-top: 20px;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
.hero {
|
|
56
|
-
background-color: white;
|
|
57
|
-
padding: 2rem;
|
|
58
|
-
border-radius: 5px;
|
|
59
|
-
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
.features {
|
|
63
|
-
background-color: white;
|
|
64
|
-
padding: 2rem;
|
|
65
|
-
margin-top: 20px;
|
|
66
|
-
border-radius: 5px;
|
|
67
|
-
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
.form-group {
|
|
71
|
-
margin-bottom: 15px;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
.form-group label {
|
|
75
|
-
display: block;
|
|
76
|
-
margin-bottom: 5px;
|
|
77
|
-
font-weight: bold;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
.form-group input,
|
|
81
|
-
.form-group textarea {
|
|
82
|
-
width: 100%;
|
|
83
|
-
padding: 8px;
|
|
84
|
-
border: 1px solid #ccc;
|
|
85
|
-
border-radius: 4px;
|
|
86
|
-
box-sizing: border-box;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
button {
|
|
90
|
-
background-color: #007bff;
|
|
91
|
-
color: white;
|
|
92
|
-
padding: 10px 20px;
|
|
93
|
-
border: none;
|
|
94
|
-
border-radius: 4px;
|
|
95
|
-
cursor: pointer;
|
|
96
|
-
font-size: 16px;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
button:hover {
|
|
100
|
-
background-color: #0056b3;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
footer {
|
|
104
|
-
margin-top: 30px;
|
|
105
|
-
text-align: center;
|
|
106
|
-
padding: 20px;
|
|
107
|
-
color: #666;
|
|
108
|
-
border-top: 1px solid #ddd;
|
|
109
|
-
}`;
|
|
110
|
-
|
|
111
|
-
res.writeHead(200);
|
|
112
|
-
res.end(css);
|
|
113
|
-
},
|
|
114
|
-
|
|
115
|
-
getJS: (req, res) => {
|
|
116
|
-
const js = `
|
|
117
|
-
document.addEventListener('DOMContentLoaded', function() {
|
|
118
|
-
const contactForm = document.getElementById('contactForm');
|
|
119
|
-
|
|
120
|
-
if (contactForm) {
|
|
121
|
-
contactForm.addEventListener('submit', function(e) {
|
|
122
|
-
e.preventDefault();
|
|
123
|
-
|
|
124
|
-
const formData = new FormData(contactForm);
|
|
125
|
-
const data = Object.fromEntries(formData);
|
|
126
|
-
|
|
127
|
-
// Enviar datos al servidor
|
|
128
|
-
fetch('/api/contact', {
|
|
129
|
-
method: 'POST',
|
|
130
|
-
headers: {
|
|
131
|
-
'Content-Type': 'application/json'
|
|
132
|
-
},
|
|
133
|
-
body: JSON.stringify(data)
|
|
134
|
-
})
|
|
135
|
-
.then(response => response.json())
|
|
136
|
-
.then(result => {
|
|
137
|
-
const responseDiv = document.getElementById('response');
|
|
138
|
-
|
|
139
|
-
if (result.success) {
|
|
140
|
-
responseDiv.innerHTML = '<p style="color: green;">' + result.message + '</p>';
|
|
141
|
-
contactForm.reset();
|
|
142
|
-
} else {
|
|
143
|
-
responseDiv.innerHTML = '<p style="color: red;">Error: ' + result.message + '</p>';
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Ocultar mensaje después de 5 segundos
|
|
147
|
-
setTimeout(() => {
|
|
148
|
-
responseDiv.innerHTML = '';
|
|
149
|
-
}, 5000);
|
|
150
|
-
})
|
|
151
|
-
.catch(error => {
|
|
152
|
-
console.error('Error:', error);
|
|
153
|
-
const responseDiv = document.getElementById('response');
|
|
154
|
-
responseDiv.innerHTML = '<p style="color: red;">Error de conexión</p>';
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
console.log('API Frontend Demo cargado correctamente');
|
|
160
|
-
});`;
|
|
161
|
-
|
|
162
|
-
res.writeHead(200);
|
|
163
|
-
res.end(js);
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
module.exports = staticController;
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
[
|
|
2
|
-
{
|
|
3
|
-
"path": "/",
|
|
4
|
-
"method": "GET",
|
|
5
|
-
"controller": "./controllers/pageController.js",
|
|
6
|
-
"handler": "homePage",
|
|
7
|
-
"auth": "none",
|
|
8
|
-
"contentType": "text/html"
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
"path": "/about",
|
|
12
|
-
"method": "GET",
|
|
13
|
-
"controller": "./controllers/pageController.js",
|
|
14
|
-
"handler": "aboutPage",
|
|
15
|
-
"auth": "none",
|
|
16
|
-
"contentType": "text/html"
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
"path": "/contact",
|
|
20
|
-
"method": "GET",
|
|
21
|
-
"controller": "./controllers/pageController.js",
|
|
22
|
-
"handler": "contactPage",
|
|
23
|
-
"auth": "none",
|
|
24
|
-
"contentType": "text/html"
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
"path": "/login",
|
|
28
|
-
"method": "GET",
|
|
29
|
-
"controller": "./controllers/authController.js",
|
|
30
|
-
"handler": "showLoginPage",
|
|
31
|
-
"auth": "none",
|
|
32
|
-
"contentType": "text/html"
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
"path": "/messages",
|
|
36
|
-
"method": "GET",
|
|
37
|
-
"controller": "./controllers/authController.js",
|
|
38
|
-
"handler": "showMessages",
|
|
39
|
-
"auth": "session",
|
|
40
|
-
"contentType": "text/html"
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
"path": "/api/login",
|
|
44
|
-
"method": "POST",
|
|
45
|
-
"controller": "./controllers/authController.js",
|
|
46
|
-
"handler": "processLogin",
|
|
47
|
-
"auth": "none",
|
|
48
|
-
"contentType": "application/json"
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
"path": "/api/contact",
|
|
52
|
-
"method": "POST",
|
|
53
|
-
"controller": "./controllers/formController.js",
|
|
54
|
-
"handler": "processContactForm",
|
|
55
|
-
"auth": "none",
|
|
56
|
-
"contentType": "application/json"
|
|
57
|
-
},
|
|
58
|
-
{
|
|
59
|
-
"path": "/api/users",
|
|
60
|
-
"method": "GET",
|
|
61
|
-
"controller": "./controllers/apiController.js",
|
|
62
|
-
"handler": "getUsers",
|
|
63
|
-
"auth": "none",
|
|
64
|
-
"contentType": "application/json"
|
|
65
|
-
},
|
|
66
|
-
{
|
|
67
|
-
"path": "/api/users/:id",
|
|
68
|
-
"method": "GET",
|
|
69
|
-
"controller": "./controllers/apiController.js",
|
|
70
|
-
"handler": "getUserById",
|
|
71
|
-
"auth": "none",
|
|
72
|
-
"contentType": "application/json"
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
"path": "/styles.css",
|
|
76
|
-
"method": "GET",
|
|
77
|
-
"controller": "./controllers/staticController.js",
|
|
78
|
-
"handler": "getCSS",
|
|
79
|
-
"auth": "none",
|
|
80
|
-
"contentType": "text/css"
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
"path": "/script.js",
|
|
84
|
-
"method": "GET",
|
|
85
|
-
"controller": "./controllers/staticController.js",
|
|
86
|
-
"handler": "getJS",
|
|
87
|
-
"auth": "none",
|
|
88
|
-
"contentType": "application/javascript"
|
|
89
|
-
}
|
|
90
|
-
]
|
package/examples/hooks/app.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Aplicación de ejemplo usando el framework JERK
|
|
3
|
-
* Demostrando routes.json, controladores y sistema de hooks
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const jerk = require('jerkjs');
|
|
7
|
-
const {
|
|
8
|
-
APIServer,
|
|
9
|
-
Logger,
|
|
10
|
-
RouteLoader,
|
|
11
|
-
hooks
|
|
12
|
-
} = jerk;
|
|
13
|
-
|
|
14
|
-
// Crear instancia del logger
|
|
15
|
-
const logger = new Logger({ level: 'info' });
|
|
16
|
-
|
|
17
|
-
// Array para almacenar los controladores disponibles
|
|
18
|
-
let availableControllers = [];
|
|
19
|
-
|
|
20
|
-
// Hook que se ejecuta cuando se carga un controlador
|
|
21
|
-
hooks.addAction('post_controller_load', (controllerModule, absolutePath) => {
|
|
22
|
-
logger.info(`[[HOOK]] - Controlador cargado: ${absolutePath}`);
|
|
23
|
-
|
|
24
|
-
// Extraer información sobre las funciones disponibles en el controlador
|
|
25
|
-
const controllerName = absolutePath.split('/').pop().replace('.js', '');
|
|
26
|
-
|
|
27
|
-
// Verificar si es un objeto con métodos o una instancia
|
|
28
|
-
let handlerNames = [];
|
|
29
|
-
if (typeof controllerModule === 'object') {
|
|
30
|
-
// Si es un objeto (como una instancia de ControllerBase), obtener sus métodos
|
|
31
|
-
handlerNames = Object.getOwnPropertyNames(Object.getPrototypeOf(controllerModule))
|
|
32
|
-
.filter(prop => typeof controllerModule[prop] === 'function' && prop !== 'constructor');
|
|
33
|
-
} else if (typeof controllerModule === 'function') {
|
|
34
|
-
// Si es una clase, instanciarla y obtener sus métodos
|
|
35
|
-
const instance = new controllerModule();
|
|
36
|
-
handlerNames = Object.getOwnPropertyNames(Object.getPrototypeOf(instance))
|
|
37
|
-
.filter(prop => typeof instance[prop] === 'function' && prop !== 'constructor');
|
|
38
|
-
} else {
|
|
39
|
-
// Si es un objeto con funciones directamente
|
|
40
|
-
handlerNames = Object.keys(controllerModule).filter(key =>
|
|
41
|
-
typeof controllerModule[key] === 'function'
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Almacenar información sobre el controlador
|
|
46
|
-
availableControllers.push({
|
|
47
|
-
name: controllerName,
|
|
48
|
-
path: absolutePath,
|
|
49
|
-
handlers: handlerNames
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
logger.info(`[[HOOK]] - Handlers disponibles en ${controllerName}:`, handlerNames);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
// Hook que se ejecuta antes de cargar rutas
|
|
56
|
-
hooks.addAction('pre_route_load', (filePath, serverInstance) => {
|
|
57
|
-
logger.info(`[[HOOK]] - A punto de cargar rutas desde: ${filePath}`);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
// Hook que se ejecuta después de cargar rutas
|
|
61
|
-
hooks.addAction('post_route_load', (routes, serverInstance) => {
|
|
62
|
-
logger.info(`[[HOOK]] - Rutas cargadas exitosamente: ${routes.length} rutas`);
|
|
63
|
-
routes.forEach((route, index) => {
|
|
64
|
-
logger.info(`[[HOOK]] - Ruta ${index + 1}: ${route.method} ${route.path} -> ${route.handlerName || 'anonymous'}`);
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// Hook para registrar accesos al servidor
|
|
69
|
-
hooks.addAction('request_received', (req, res) => {
|
|
70
|
-
logger.info(`[[ACCESS]] - Nuevo acceso recibido: ${req.method} ${req.url} desde ${req.connection.remoteAddress || 'unknown'}`);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
// Hook para registrar descargas o finalización de solicitudes
|
|
74
|
-
hooks.addAction('request_completed', (req, res) => {
|
|
75
|
-
logger.info(`[[DOWNLOAD/COMPLETED]] - Solicitud completada: ${req.method} ${req.url}`);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
async function startServer() {
|
|
79
|
-
// Crear instancia del servidor
|
|
80
|
-
const server = new APIServer({
|
|
81
|
-
port: 11000,
|
|
82
|
-
host: 'localhost'
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
try {
|
|
86
|
-
// Middleware para capturar inicio de solicitudes
|
|
87
|
-
server.use((req, res, next) => {
|
|
88
|
-
// Disparar hook de solicitud recibida
|
|
89
|
-
hooks.doAction('request_received', req, res);
|
|
90
|
-
|
|
91
|
-
// Guardar la función original de res.end
|
|
92
|
-
const originalEnd = res.end;
|
|
93
|
-
|
|
94
|
-
// Sobrescribir res.end para capturar cuando se completa la solicitud
|
|
95
|
-
res.end = function(chunk, encoding, callback) {
|
|
96
|
-
// Llamar a la función original
|
|
97
|
-
const result = originalEnd.call(this, chunk, encoding, callback);
|
|
98
|
-
|
|
99
|
-
// Disparar hook de solicitud completada
|
|
100
|
-
hooks.doAction('request_completed', req, res);
|
|
101
|
-
|
|
102
|
-
return result;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
next();
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
// Cargar rutas desde archivo JSON
|
|
109
|
-
const routeLoader = new RouteLoader();
|
|
110
|
-
await routeLoader.loadRoutes(server, './routes.json');
|
|
111
|
-
|
|
112
|
-
// Mostrar los controladores disponibles (usando hooks)
|
|
113
|
-
logger.info('\n=== CONTROLADORES DISPONIBLES ===');
|
|
114
|
-
availableControllers.forEach((controller, index) => {
|
|
115
|
-
logger.info(`[[CONTROLLER ${index + 1}]] - Nombre: ${controller.name}`);
|
|
116
|
-
logger.info(`[[PATH]] - Ruta: ${controller.path}`);
|
|
117
|
-
logger.info(`[[HANDLERS]] - Funciones: ${controller.handlers.join(', ')}`);
|
|
118
|
-
logger.info('---');
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
// Iniciar el servidor
|
|
122
|
-
server.start();
|
|
123
|
-
|
|
124
|
-
logger.info(`\nServidor iniciado en http://localhost:${server.port}`);
|
|
125
|
-
logger.info('La aplicación está lista para recibir solicitudes');
|
|
126
|
-
|
|
127
|
-
} catch (error) {
|
|
128
|
-
logger.error('Error iniciando el servidor:', error.message);
|
|
129
|
-
process.exit(1);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Iniciar el servidor
|
|
134
|
-
startServer();
|
|
135
|
-
|
|
136
|
-
module.exports = { startServer };
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Controlador de autenticación (authController)
|
|
3
|
-
* Maneja operaciones de login
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
// Datos simulados de usuarios para autenticación
|
|
7
|
-
const validUsers = [
|
|
8
|
-
{ username: 'admin', password: 'admin123' },
|
|
9
|
-
{ username: 'user', password: 'user123' }
|
|
10
|
-
];
|
|
11
|
-
|
|
12
|
-
// Función de login
|
|
13
|
-
function login(req, res) {
|
|
14
|
-
let body = '';
|
|
15
|
-
|
|
16
|
-
req.on('data', chunk => {
|
|
17
|
-
body += chunk.toString();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
req.on('end', () => {
|
|
21
|
-
try {
|
|
22
|
-
const credentials = JSON.parse(body);
|
|
23
|
-
const user = validUsers.find(u =>
|
|
24
|
-
u.username === credentials.username &&
|
|
25
|
-
u.password === credentials.password
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
if (user) {
|
|
29
|
-
// Simular generación de token JWT
|
|
30
|
-
const token = `fake-jwt-token-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
31
|
-
|
|
32
|
-
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
33
|
-
res.end(JSON.stringify({
|
|
34
|
-
success: true,
|
|
35
|
-
token: token,
|
|
36
|
-
message: 'Login exitoso'
|
|
37
|
-
}));
|
|
38
|
-
} else {
|
|
39
|
-
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
40
|
-
res.end(JSON.stringify({
|
|
41
|
-
success: false,
|
|
42
|
-
message: 'Credenciales inválidas'
|
|
43
|
-
}));
|
|
44
|
-
}
|
|
45
|
-
} catch (error) {
|
|
46
|
-
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
47
|
-
res.end(JSON.stringify({ error: 'Datos inválidos' }));
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
module.exports = {
|
|
53
|
-
login
|
|
54
|
-
};
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Controlador principal (mainController)
|
|
3
|
-
* Maneja la página de inicio
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const { ControllerBase } = require('jerkjs');
|
|
7
|
-
|
|
8
|
-
class MainController extends ControllerBase {
|
|
9
|
-
constructor(options = {}) {
|
|
10
|
-
super(options);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// Función para manejar la ruta de inicio
|
|
14
|
-
home(req, res) {
|
|
15
|
-
// Establecer variables para la vista
|
|
16
|
-
this.set('title', 'Bienvenido a JERK Framework');
|
|
17
|
-
this.set('message', 'Esta es una aplicación de ejemplo usando JERK Framework');
|
|
18
|
-
|
|
19
|
-
// Renderizar la vista usando el motor de plantillas de JERK
|
|
20
|
-
this.render(res, 'home', {
|
|
21
|
-
endpoints: [
|
|
22
|
-
{ method: 'GET', path: '/users', description: 'Obtener todos los usuarios' },
|
|
23
|
-
{ method: 'GET', path: '/users/:id', description: 'Obtener usuario por ID' },
|
|
24
|
-
{ method: 'POST', path: '/users', description: 'Crear nuevo usuario' },
|
|
25
|
-
{ method: 'GET', path: '/products', description: 'Obtener todos los productos' },
|
|
26
|
-
{ method: 'GET', path: '/products/:id', description: 'Obtener producto por ID' },
|
|
27
|
-
{ method: 'POST', path: '/login', description: 'Iniciar sesión' }
|
|
28
|
-
]
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Exportar métodos directamente para que el RouteLoader pueda acceder a ellos
|
|
34
|
-
const controllerInstance = new MainController({ viewsPath: './views' });
|
|
35
|
-
|
|
36
|
-
module.exports = {
|
|
37
|
-
home: (req, res) => {
|
|
38
|
-
controllerInstance.setRequestResponse(req, res);
|
|
39
|
-
controllerInstance.home(req, res);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Controlador de productos (productController)
|
|
3
|
-
* Maneja operaciones CRUD de productos
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
// Datos simulados de productos
|
|
7
|
-
let products = [
|
|
8
|
-
{ id: 1, name: 'Laptop', price: 1200, category: 'Electrónica' },
|
|
9
|
-
{ id: 2, name: 'Teléfono', price: 800, category: 'Electrónica' },
|
|
10
|
-
{ id: 3, name: 'Libro', price: 15, category: 'Educación' }
|
|
11
|
-
];
|
|
12
|
-
|
|
13
|
-
// Obtener todos los productos
|
|
14
|
-
function getAllProducts(req, res) {
|
|
15
|
-
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
16
|
-
res.end(JSON.stringify(products));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Obtener un producto por ID
|
|
20
|
-
function getProductById(req, res) {
|
|
21
|
-
// Obtener el ID de los parámetros de la ruta
|
|
22
|
-
const urlParts = req.url.split('/');
|
|
23
|
-
const productId = parseInt(urlParts[urlParts.length - 1]);
|
|
24
|
-
|
|
25
|
-
const product = products.find(p => p.id === productId);
|
|
26
|
-
|
|
27
|
-
if (product) {
|
|
28
|
-
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
29
|
-
res.end(JSON.stringify(product));
|
|
30
|
-
} else {
|
|
31
|
-
res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
32
|
-
res.end(JSON.stringify({ error: 'Producto no encontrado' }));
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
module.exports = {
|
|
37
|
-
getAllProducts,
|
|
38
|
-
getProductById
|
|
39
|
-
};
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Controlador de usuarios (userController)
|
|
3
|
-
* Maneja operaciones CRUD de usuarios
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
// Datos simulados de usuarios
|
|
7
|
-
let users = [
|
|
8
|
-
{ id: 1, name: 'Juan Pérez', email: 'juan@example.com', age: 30 },
|
|
9
|
-
{ id: 2, name: 'María García', email: 'maria@example.com', age: 25 },
|
|
10
|
-
{ id: 3, name: 'Pedro Rodríguez', email: 'pedro@example.com', age: 35 }
|
|
11
|
-
];
|
|
12
|
-
|
|
13
|
-
// Obtener todos los usuarios
|
|
14
|
-
function getAllUsers(req, res) {
|
|
15
|
-
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
16
|
-
res.end(JSON.stringify(users));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Obtener un usuario por ID
|
|
20
|
-
function getUserById(req, res) {
|
|
21
|
-
// Obtener el ID de los parámetros de la ruta
|
|
22
|
-
const urlParts = req.url.split('/');
|
|
23
|
-
const userId = parseInt(urlParts[urlParts.length - 1]);
|
|
24
|
-
|
|
25
|
-
const user = users.find(u => u.id === userId);
|
|
26
|
-
|
|
27
|
-
if (user) {
|
|
28
|
-
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
29
|
-
res.end(JSON.stringify(user));
|
|
30
|
-
} else {
|
|
31
|
-
res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
32
|
-
res.end(JSON.stringify({ error: 'Usuario no encontrado' }));
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// Crear un nuevo usuario
|
|
37
|
-
function createUser(req, res) {
|
|
38
|
-
let body = '';
|
|
39
|
-
|
|
40
|
-
req.on('data', chunk => {
|
|
41
|
-
body += chunk.toString();
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
req.on('end', () => {
|
|
45
|
-
try {
|
|
46
|
-
const userData = JSON.parse(body);
|
|
47
|
-
const newUser = {
|
|
48
|
-
id: users.length + 1,
|
|
49
|
-
name: userData.name,
|
|
50
|
-
email: userData.email,
|
|
51
|
-
age: userData.age
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
users.push(newUser);
|
|
55
|
-
|
|
56
|
-
res.writeHead(201, { 'Content-Type': 'application/json' });
|
|
57
|
-
res.end(JSON.stringify(newUser));
|
|
58
|
-
} catch (error) {
|
|
59
|
-
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
60
|
-
res.end(JSON.stringify({ error: 'Datos inválidos' }));
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
module.exports = {
|
|
66
|
-
getAllUsers,
|
|
67
|
-
getUserById,
|
|
68
|
-
createUser
|
|
69
|
-
};
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
[
|
|
2
|
-
{
|
|
3
|
-
"method": "GET",
|
|
4
|
-
"path": "/",
|
|
5
|
-
"controller": "./controllers/mainController.js",
|
|
6
|
-
"handler": "home",
|
|
7
|
-
"auth": "none"
|
|
8
|
-
},
|
|
9
|
-
{
|
|
10
|
-
"method": "GET",
|
|
11
|
-
"path": "/users",
|
|
12
|
-
"controller": "./controllers/userController.js",
|
|
13
|
-
"handler": "getAllUsers",
|
|
14
|
-
"auth": "none"
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
"method": "GET",
|
|
18
|
-
"path": "/users/:id",
|
|
19
|
-
"controller": "./controllers/userController.js",
|
|
20
|
-
"handler": "getUserById",
|
|
21
|
-
"auth": "none"
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
"method": "POST",
|
|
25
|
-
"path": "/users",
|
|
26
|
-
"controller": "./controllers/userController.js",
|
|
27
|
-
"handler": "createUser",
|
|
28
|
-
"auth": "required"
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
"method": "GET",
|
|
32
|
-
"path": "/products",
|
|
33
|
-
"controller": "./controllers/productController.js",
|
|
34
|
-
"handler": "getAllProducts",
|
|
35
|
-
"auth": "none"
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
"method": "GET",
|
|
39
|
-
"path": "/products/:id",
|
|
40
|
-
"controller": "./controllers/productController.js",
|
|
41
|
-
"handler": "getProductById",
|
|
42
|
-
"auth": "none"
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
"method": "POST",
|
|
46
|
-
"path": "/login",
|
|
47
|
-
"controller": "./controllers/authController.js",
|
|
48
|
-
"handler": "login",
|
|
49
|
-
"auth": "none"
|
|
50
|
-
}
|
|
51
|
-
]
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="es">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8">
|
|
5
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
-
<title>{{title}}</title>
|
|
7
|
-
<style>
|
|
8
|
-
body {
|
|
9
|
-
font-family: Arial, sans-serif;
|
|
10
|
-
margin: 40px;
|
|
11
|
-
background-color: #f5f5f5;
|
|
12
|
-
}
|
|
13
|
-
.container {
|
|
14
|
-
max-width: 800px;
|
|
15
|
-
margin: 0 auto;
|
|
16
|
-
background: white;
|
|
17
|
-
padding: 20px;
|
|
18
|
-
border-radius: 8px;
|
|
19
|
-
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
|
20
|
-
}
|
|
21
|
-
h1 {
|
|
22
|
-
color: #333;
|
|
23
|
-
}
|
|
24
|
-
.api-info {
|
|
25
|
-
margin-top: 20px;
|
|
26
|
-
padding: 15px;
|
|
27
|
-
background-color: #e7f3ff;
|
|
28
|
-
border-left: 4px solid #2196F3;
|
|
29
|
-
}
|
|
30
|
-
.endpoint-item {
|
|
31
|
-
margin: 5px 0;
|
|
32
|
-
}
|
|
33
|
-
</style>
|
|
34
|
-
</head>
|
|
35
|
-
<body>
|
|
36
|
-
<div class="container">
|
|
37
|
-
<h1>{{title}}</h1>
|
|
38
|
-
<p>{{message}}</p>
|
|
39
|
-
|
|
40
|
-
<div class="api-info">
|
|
41
|
-
<h2>Endpoints disponibles:</h2>
|
|
42
|
-
<ul>
|
|
43
|
-
{{foreach:endpoints}}
|
|
44
|
-
<li class="endpoint-item"><strong>{{item.method}}</strong> {{item.path}} - {{item.description}}</li>
|
|
45
|
-
{{endforeach}}
|
|
46
|
-
</ul>
|
|
47
|
-
</div>
|
|
48
|
-
</div>
|
|
49
|
-
</body>
|
|
50
|
-
</html>
|