@gieo/express 1.0.7 → 1.0.8

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 CHANGED
@@ -1,7 +1,13 @@
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
12
  constructor() {
7
13
  this.routes = {
@@ -12,6 +18,10 @@ class ExpressPlus {
12
18
  DELETE: [],
13
19
  };
14
20
  this.middlewares = [];
21
+ // Định nghĩa thư mục chứa views (mặc định là "views")
22
+ this.viewsPath = path_1.default.join(process.cwd(), "views");
23
+ // Có thể cho phép tùy chỉnh viewsPath qua constructor nếu cần
24
+ // this.viewsPath = options.viewsPath || path.join(process.cwd(), "views");
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
+ extendedRes.status(404).end("View not found");
86
+ return;
87
+ }
88
+ const html = ejs_1.default.render(fs_1.default.readFileSync(filePath, "utf-8"), data);
89
+ extendedRes.setHeader("Content-Type", "text/html");
90
+ extendedRes.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.7",
3
+ "version": "1.0.8",
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,6 +1,8 @@
1
1
  import { createServer, IncomingMessage, ServerResponse } from "http";
2
2
  import { parse } from "url";
3
-
3
+ import ejs from "ejs";
4
+ import path from "path";
5
+ import fs from "fs";
4
6
  // Mở rộng Request
5
7
  interface ExtendedRequest extends IncomingMessage {
6
8
  query?: Record<string, string | string[]>;
@@ -14,6 +16,7 @@ interface ExtendedRequest extends IncomingMessage {
14
16
  interface ExtendedResponse extends ServerResponse {
15
17
  status: (code: number) => ExtendedResponse;
16
18
  json: (data: any) => void;
19
+ render: (view: string, data?: any) => void; // Thêm phương thức render
17
20
  }
18
21
 
19
22
  type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
@@ -39,6 +42,14 @@ class ExpressPlus {
39
42
  next: () => void
40
43
  ) => void)[] = [];
41
44
 
45
+ // Định nghĩa thư mục chứa views (mặc định là "views")
46
+ private viewsPath: string = path.join(process.cwd(), "views");
47
+
48
+ constructor() {
49
+ // Có thể cho phép tùy chỉnh viewsPath qua constructor nếu cần
50
+ // this.viewsPath = options.viewsPath || path.join(process.cwd(), "views");
51
+ }
52
+
42
53
  private addRoute(method: Method, path: string, handler: Handler): void {
43
54
  this.routes[method].push({ path, handler });
44
55
  }
@@ -74,7 +85,7 @@ class ExpressPlus {
74
85
  }
75
86
 
76
87
  listen(port: number, callback: () => void): void {
77
- const server = createServer((req, res) => {
88
+ const server = createServer(async (req, res) => {
78
89
  const method = req.method as Method;
79
90
  const parsedUrl = parse(req.url || "", true);
80
91
  const pathname = parsedUrl.pathname || "/";
@@ -114,6 +125,18 @@ class ExpressPlus {
114
125
  extendedRes.end(JSON.stringify(data));
115
126
  };
116
127
 
128
+ // Thêm phương thức render cho EJS
129
+ extendedRes.render = function (view: string, data: any = {}) {
130
+ const filePath = path.join(this.viewsPath, `${view}.ejs`);
131
+ if (!fs.existsSync(filePath)) {
132
+ extendedRes.status(404).end("View not found");
133
+ return;
134
+ }
135
+ const html = ejs.render(fs.readFileSync(filePath, "utf-8"), data);
136
+ extendedRes.setHeader("Content-Type", "text/html");
137
+ extendedRes.end(html);
138
+ };
139
+
117
140
  // 🔍 Tìm route phù hợp (hỗ trợ dynamic route)
118
141
  const route = this.routes[method].find((r) => {
119
142
  const routeParts = r.path.split("/").filter(Boolean);
@@ -163,3 +186,4 @@ class ExpressPlus {
163
186
  }
164
187
 
165
188
  export default ExpressPlus;
189
+ export { ExtendedRequest, ExtendedResponse };