@gieo/express 1.0.8 → 1.0.10
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/dist/index.js +17 -17
- package/package.json +1 -1
- package/src/index.ts +23 -18
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ const ejs_1 = __importDefault(require("ejs"));
|
|
|
9
9
|
const path_1 = __importDefault(require("path"));
|
|
10
10
|
const fs_1 = __importDefault(require("fs"));
|
|
11
11
|
class ExpressPlus {
|
|
12
|
-
constructor() {
|
|
12
|
+
constructor(options = {}) {
|
|
13
13
|
this.routes = {
|
|
14
14
|
GET: [],
|
|
15
15
|
POST: [],
|
|
@@ -18,10 +18,10 @@ class ExpressPlus {
|
|
|
18
18
|
DELETE: [],
|
|
19
19
|
};
|
|
20
20
|
this.middlewares = [];
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
this.viewsPath = options.viewsPath || path_1.default.join(process.cwd(), "views");
|
|
22
|
+
if (!fs_1.default.existsSync(this.viewsPath)) {
|
|
23
|
+
throw new Error(`Views directory "${this.viewsPath}" does not exist`);
|
|
24
|
+
}
|
|
25
25
|
}
|
|
26
26
|
addRoute(method, path, handler) {
|
|
27
27
|
this.routes[method].push({ path, handler });
|
|
@@ -71,24 +71,24 @@ class ExpressPlus {
|
|
|
71
71
|
return extendedReq.body;
|
|
72
72
|
};
|
|
73
73
|
extendedRes.status = function (code) {
|
|
74
|
-
|
|
75
|
-
return
|
|
76
|
-
};
|
|
74
|
+
this.statusCode = code;
|
|
75
|
+
return this;
|
|
76
|
+
}.bind(this);
|
|
77
77
|
extendedRes.json = function (data) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
};
|
|
81
|
-
//
|
|
78
|
+
this.setHeader("Content-Type", "application/json");
|
|
79
|
+
this.end(JSON.stringify(data));
|
|
80
|
+
}.bind(this);
|
|
81
|
+
// Bind render method with the correct context
|
|
82
82
|
extendedRes.render = function (view, data = {}) {
|
|
83
|
-
const filePath = path_1.default.join(this.viewsPath, `${view}.ejs`);
|
|
83
|
+
const filePath = path_1.default.join(this.viewsPath, `${view}.ejs`); // Sử dụng this.viewsPath từ instance
|
|
84
84
|
if (!fs_1.default.existsSync(filePath)) {
|
|
85
|
-
|
|
85
|
+
this.status(404).end(`View "${view}.ejs" not found in ${this.viewsPath}`);
|
|
86
86
|
return;
|
|
87
87
|
}
|
|
88
88
|
const html = ejs_1.default.render(fs_1.default.readFileSync(filePath, "utf-8"), data);
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
};
|
|
89
|
+
this.setHeader("Content-Type", "text/html");
|
|
90
|
+
this.end(html);
|
|
91
|
+
}.bind(this); // Bind this để giữ context của ExpressPlus
|
|
92
92
|
// 🔍 Tìm route phù hợp (hỗ trợ dynamic route)
|
|
93
93
|
const route = this.routes[method].find((r) => {
|
|
94
94
|
const routeParts = r.path.split("/").filter(Boolean);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { parse } from "url";
|
|
|
3
3
|
import ejs from "ejs";
|
|
4
4
|
import path from "path";
|
|
5
5
|
import fs from "fs";
|
|
6
|
+
|
|
6
7
|
// Mở rộng Request
|
|
7
8
|
interface ExtendedRequest extends IncomingMessage {
|
|
8
9
|
query?: Record<string, string | string[]>;
|
|
@@ -16,7 +17,7 @@ interface ExtendedRequest extends IncomingMessage {
|
|
|
16
17
|
interface ExtendedResponse extends ServerResponse {
|
|
17
18
|
status: (code: number) => ExtendedResponse;
|
|
18
19
|
json: (data: any) => void;
|
|
19
|
-
render: (view: string, data?: any) => void;
|
|
20
|
+
render: (view: string, data?: any) => void;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
@@ -42,12 +43,14 @@ class ExpressPlus {
|
|
|
42
43
|
next: () => void
|
|
43
44
|
) => void)[] = [];
|
|
44
45
|
|
|
45
|
-
//
|
|
46
|
-
private viewsPath: string
|
|
46
|
+
// Lưu viewsPath như một thuộc tính của instance
|
|
47
|
+
private viewsPath: string;
|
|
47
48
|
|
|
48
|
-
constructor() {
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
constructor(options: { viewsPath?: string } = {}) {
|
|
50
|
+
this.viewsPath = options.viewsPath || path.join(process.cwd(), "views");
|
|
51
|
+
if (!fs.existsSync(this.viewsPath)) {
|
|
52
|
+
throw new Error(`Views directory "${this.viewsPath}" does not exist`);
|
|
53
|
+
}
|
|
51
54
|
}
|
|
52
55
|
|
|
53
56
|
private addRoute(method: Method, path: string, handler: Handler): void {
|
|
@@ -116,26 +119,28 @@ class ExpressPlus {
|
|
|
116
119
|
};
|
|
117
120
|
|
|
118
121
|
extendedRes.status = function (code: number) {
|
|
119
|
-
|
|
120
|
-
return
|
|
121
|
-
};
|
|
122
|
+
this.statusCode = code;
|
|
123
|
+
return this;
|
|
124
|
+
}.bind(this);
|
|
122
125
|
|
|
123
126
|
extendedRes.json = function (data: any) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
};
|
|
127
|
+
this.setHeader("Content-Type", "application/json");
|
|
128
|
+
this.end(JSON.stringify(data));
|
|
129
|
+
}.bind(this);
|
|
127
130
|
|
|
128
|
-
//
|
|
131
|
+
// Bind render method with the correct context
|
|
129
132
|
extendedRes.render = function (view: string, data: any = {}) {
|
|
130
|
-
const filePath = path.join(this.viewsPath, `${view}.ejs`);
|
|
133
|
+
const filePath = path.join(this.viewsPath, `${view}.ejs`); // Sử dụng this.viewsPath từ instance
|
|
131
134
|
if (!fs.existsSync(filePath)) {
|
|
132
|
-
|
|
135
|
+
this.status(404).end(
|
|
136
|
+
`View "${view}.ejs" not found in ${this.viewsPath}`
|
|
137
|
+
);
|
|
133
138
|
return;
|
|
134
139
|
}
|
|
135
140
|
const html = ejs.render(fs.readFileSync(filePath, "utf-8"), data);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
};
|
|
141
|
+
this.setHeader("Content-Type", "text/html");
|
|
142
|
+
this.end(html);
|
|
143
|
+
}.bind(this); // Bind this để giữ context của ExpressPlus
|
|
139
144
|
|
|
140
145
|
// 🔍 Tìm route phù hợp (hỗ trợ dynamic route)
|
|
141
146
|
const route = this.routes[method].find((r) => {
|