@gieo/express 1.0.8 → 1.0.9
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 +8 -8
- package/package.json +1 -1
- package/src/index.ts +14 -9
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 });
|
|
@@ -82,12 +82,12 @@ class ExpressPlus {
|
|
|
82
82
|
extendedRes.render = function (view, data = {}) {
|
|
83
83
|
const filePath = path_1.default.join(this.viewsPath, `${view}.ejs`);
|
|
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
|
-
|
|
89
|
+
this.setHeader("Content-Type", "text/html");
|
|
90
|
+
this.end(html);
|
|
91
91
|
};
|
|
92
92
|
// 🔍 Tìm route phù hợp (hỗ trợ dynamic route)
|
|
93
93
|
const route = this.routes[method].find((r) => {
|
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
|
-
// Định nghĩa thư mục chứa views
|
|
46
|
-
private viewsPath: string
|
|
46
|
+
// Định nghĩa thư mục chứa views với giá trị mặc định và cho phép tùy chỉnh
|
|
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 {
|
|
@@ -129,12 +132,14 @@ class ExpressPlus {
|
|
|
129
132
|
extendedRes.render = function (view: string, data: any = {}) {
|
|
130
133
|
const filePath = path.join(this.viewsPath, `${view}.ejs`);
|
|
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
|
-
|
|
141
|
+
this.setHeader("Content-Type", "text/html");
|
|
142
|
+
this.end(html);
|
|
138
143
|
};
|
|
139
144
|
|
|
140
145
|
// 🔍 Tìm route phù hợp (hỗ trợ dynamic route)
|