@gieo/express 1.0.7 → 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 +23 -2
- package/package.json +4 -1
- package/src/index.ts +30 -1
package/dist/index.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
const http_1 = require("http");
|
|
4
7
|
const url_1 = require("url");
|
|
8
|
+
const ejs_1 = __importDefault(require("ejs"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const fs_1 = __importDefault(require("fs"));
|
|
5
11
|
class ExpressPlus {
|
|
6
|
-
constructor() {
|
|
12
|
+
constructor(options = {}) {
|
|
7
13
|
this.routes = {
|
|
8
14
|
GET: [],
|
|
9
15
|
POST: [],
|
|
@@ -12,6 +18,10 @@ class ExpressPlus {
|
|
|
12
18
|
DELETE: [],
|
|
13
19
|
};
|
|
14
20
|
this.middlewares = [];
|
|
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
|
+
}
|
|
15
25
|
}
|
|
16
26
|
addRoute(method, path, handler) {
|
|
17
27
|
this.routes[method].push({ path, handler });
|
|
@@ -35,7 +45,7 @@ class ExpressPlus {
|
|
|
35
45
|
this.addRoute("DELETE", path, handler);
|
|
36
46
|
}
|
|
37
47
|
listen(port, callback) {
|
|
38
|
-
const server = (0, http_1.createServer)((req, res) => {
|
|
48
|
+
const server = (0, http_1.createServer)(async (req, res) => {
|
|
39
49
|
const method = req.method;
|
|
40
50
|
const parsedUrl = (0, url_1.parse)(req.url || "", true);
|
|
41
51
|
const pathname = parsedUrl.pathname || "/";
|
|
@@ -68,6 +78,17 @@ class ExpressPlus {
|
|
|
68
78
|
extendedRes.setHeader("Content-Type", "application/json");
|
|
69
79
|
extendedRes.end(JSON.stringify(data));
|
|
70
80
|
};
|
|
81
|
+
// Thêm phương thức render cho EJS
|
|
82
|
+
extendedRes.render = function (view, data = {}) {
|
|
83
|
+
const filePath = path_1.default.join(this.viewsPath, `${view}.ejs`);
|
|
84
|
+
if (!fs_1.default.existsSync(filePath)) {
|
|
85
|
+
this.status(404).end(`View "${view}.ejs" not found in ${this.viewsPath}`);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const html = ejs_1.default.render(fs_1.default.readFileSync(filePath, "utf-8"), data);
|
|
89
|
+
this.setHeader("Content-Type", "text/html");
|
|
90
|
+
this.end(html);
|
|
91
|
+
};
|
|
71
92
|
// 🔍 Tìm route phù hợp (hỗ trợ dynamic route)
|
|
72
93
|
const route = this.routes[method].find((r) => {
|
|
73
94
|
const routeParts = r.path.split("/").filter(Boolean);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gieo/express",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"keywords": [],
|
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
"jiti": "^2.5.1",
|
|
13
13
|
"typescript": "^5.9.2"
|
|
14
14
|
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"ejs": "^3.1.10"
|
|
17
|
+
},
|
|
15
18
|
"scripts": {
|
|
16
19
|
"test": "node dist/test.js"
|
|
17
20
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { createServer, IncomingMessage, ServerResponse } from "http";
|
|
2
2
|
import { parse } from "url";
|
|
3
|
+
import ejs from "ejs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import fs from "fs";
|
|
3
6
|
|
|
4
7
|
// Mở rộng Request
|
|
5
8
|
interface ExtendedRequest extends IncomingMessage {
|
|
@@ -14,6 +17,7 @@ interface ExtendedRequest extends IncomingMessage {
|
|
|
14
17
|
interface ExtendedResponse extends ServerResponse {
|
|
15
18
|
status: (code: number) => ExtendedResponse;
|
|
16
19
|
json: (data: any) => void;
|
|
20
|
+
render: (view: string, data?: any) => void;
|
|
17
21
|
}
|
|
18
22
|
|
|
19
23
|
type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
@@ -39,6 +43,16 @@ class ExpressPlus {
|
|
|
39
43
|
next: () => void
|
|
40
44
|
) => void)[] = [];
|
|
41
45
|
|
|
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;
|
|
48
|
+
|
|
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
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
42
56
|
private addRoute(method: Method, path: string, handler: Handler): void {
|
|
43
57
|
this.routes[method].push({ path, handler });
|
|
44
58
|
}
|
|
@@ -74,7 +88,7 @@ class ExpressPlus {
|
|
|
74
88
|
}
|
|
75
89
|
|
|
76
90
|
listen(port: number, callback: () => void): void {
|
|
77
|
-
const server = createServer((req, res) => {
|
|
91
|
+
const server = createServer(async (req, res) => {
|
|
78
92
|
const method = req.method as Method;
|
|
79
93
|
const parsedUrl = parse(req.url || "", true);
|
|
80
94
|
const pathname = parsedUrl.pathname || "/";
|
|
@@ -114,6 +128,20 @@ class ExpressPlus {
|
|
|
114
128
|
extendedRes.end(JSON.stringify(data));
|
|
115
129
|
};
|
|
116
130
|
|
|
131
|
+
// Thêm phương thức render cho EJS
|
|
132
|
+
extendedRes.render = function (view: string, data: any = {}) {
|
|
133
|
+
const filePath = path.join(this.viewsPath, `${view}.ejs`);
|
|
134
|
+
if (!fs.existsSync(filePath)) {
|
|
135
|
+
this.status(404).end(
|
|
136
|
+
`View "${view}.ejs" not found in ${this.viewsPath}`
|
|
137
|
+
);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const html = ejs.render(fs.readFileSync(filePath, "utf-8"), data);
|
|
141
|
+
this.setHeader("Content-Type", "text/html");
|
|
142
|
+
this.end(html);
|
|
143
|
+
};
|
|
144
|
+
|
|
117
145
|
// 🔍 Tìm route phù hợp (hỗ trợ dynamic route)
|
|
118
146
|
const route = this.routes[method].find((r) => {
|
|
119
147
|
const routeParts = r.path.split("/").filter(Boolean);
|
|
@@ -163,3 +191,4 @@ class ExpressPlus {
|
|
|
163
191
|
}
|
|
164
192
|
|
|
165
193
|
export default ExpressPlus;
|
|
194
|
+
export { ExtendedRequest, ExtendedResponse };
|