@gieo/express 1.0.0 → 1.0.1

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
@@ -11,10 +11,14 @@ var ExpressPlus = /** @class */ (function () {
11
11
  PATCH: [],
12
12
  DELETE: [],
13
13
  };
14
+ this.middlewares = [];
14
15
  }
15
16
  ExpressPlus.prototype.addRoute = function (method, path, handler) {
16
17
  this.routes[method].push({ path: path, handler: handler });
17
18
  };
19
+ ExpressPlus.prototype.use = function (middleware) {
20
+ this.middlewares.push(middleware);
21
+ };
18
22
  ExpressPlus.prototype.get = function (path, handler) {
19
23
  this.addRoute("GET", path, handler);
20
24
  };
@@ -37,14 +41,23 @@ var ExpressPlus = /** @class */ (function () {
37
41
  var parsedUrl = (0, url_1.parse)(req.url || "", true);
38
42
  var pathname = parsedUrl.pathname || "/";
39
43
  var route = _this.routes[method].find(function (r) { return r.path === pathname; });
40
- if (route) {
41
- req.query = parsedUrl.query;
42
- route.handler(req, res);
43
- }
44
- else {
45
- res.statusCode = 404;
46
- res.end("Not Found");
47
- }
44
+ req.query = parsedUrl.query;
45
+ req.body = {};
46
+ var i = 0;
47
+ var next = function () {
48
+ var middleware = _this.middlewares[i++];
49
+ if (middleware) {
50
+ middleware(req, res, next);
51
+ }
52
+ else if (route) {
53
+ route.handler(req, res);
54
+ }
55
+ else {
56
+ res.statusCode = 404;
57
+ res.end("Not Found");
58
+ }
59
+ };
60
+ next();
48
61
  });
49
62
  server.listen(port, callback);
50
63
  };
package/dist/test.js CHANGED
@@ -1,23 +1,62 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var index_1 = require("./index");
4
+ var utils_1 = require("@gieo/utils");
4
5
  var app = new index_1.default();
6
+ // GET /
5
7
  app.get("/", function (req, res) {
6
8
  res.end("Hello, World!");
7
9
  });
10
+ // GET /hello?name=...
8
11
  app.get("/hello", function (req, res) {
9
12
  var name = req.query.name || "Guest";
10
13
  res.end("Hello, ".concat(name, "!"));
11
14
  });
12
- app.post("/data", function (req, res) {
15
+ // Middleware để parse body JSON
16
+ app.use(function (req, res, next) {
13
17
  var body = "";
14
18
  req.on("data", function (chunk) {
15
19
  body += chunk.toString();
16
20
  });
17
21
  req.on("end", function () {
18
- res.end("Received data: ".concat(body));
22
+ try {
23
+ req.body = JSON.parse(body);
24
+ }
25
+ catch (e) {
26
+ req.body = {};
27
+ }
28
+ next();
19
29
  });
20
30
  });
31
+ // POST /calculate
32
+ app.post("/calculate", function (req, res) {
33
+ var _a = req.body, a = _a.a, b = _a.b, op = _a.op;
34
+ if (typeof a !== "number" || typeof b !== "number") {
35
+ res.statusCode = 400;
36
+ return res.end("a và b phải là số");
37
+ }
38
+ var result;
39
+ switch (op) {
40
+ case "sum":
41
+ result = (0, utils_1.sum)(a, b);
42
+ break;
43
+ case "sub":
44
+ result = (0, utils_1.sub)(a, b);
45
+ break;
46
+ case "mul":
47
+ result = (0, utils_1.mul)(a, b);
48
+ break;
49
+ case "div":
50
+ result = (0, utils_1.div)(a, b);
51
+ break;
52
+ default:
53
+ res.statusCode = 400;
54
+ return res.end("Toán tử không hợp lệ (sum, sub, mul, div)");
55
+ }
56
+ res.setHeader("Content-Type", "application/json");
57
+ res.end(JSON.stringify({ result: result }));
58
+ });
59
+ // Các route khác
21
60
  app.put("/update", function (req, res) {
22
61
  res.end("Update successful");
23
62
  });
@@ -27,6 +66,7 @@ app.patch("/modify", function (req, res) {
27
66
  app.delete("/delete", function (req, res) {
28
67
  res.end("Delete successful");
29
68
  });
69
+ // Start server
30
70
  app.listen(3000, function () {
31
71
  console.log("Server is running on http://localhost:3000");
32
72
  });
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@gieo/express",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "keywords": [],
7
7
  "author": "",
8
8
  "license": "ISC",
9
9
  "devDependencies": {
10
+ "@gieo/utils": "^1.0.5",
10
11
  "@types/node": "^24.2.0",
11
12
  "jiti": "^2.5.1",
12
13
  "typescript": "^5.9.2"
package/src/index.ts CHANGED
@@ -3,7 +3,7 @@ import { parse } from "url";
3
3
 
4
4
  type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
5
5
  type Handler = (
6
- req: IncomingMessage & { query?: any },
6
+ req: IncomingMessage & { query?: any; body?: any },
7
7
  res: ServerResponse
8
8
  ) => void;
9
9
 
@@ -21,10 +21,26 @@ export default class ExpressPlus {
21
21
  DELETE: [],
22
22
  };
23
23
 
24
+ private middlewares: ((
25
+ req: IncomingMessage & { query?: any; body?: any },
26
+ res: ServerResponse,
27
+ next: () => void
28
+ ) => void)[] = [];
29
+
24
30
  private addRoute(method: Method, path: string, handler: Handler): void {
25
31
  this.routes[method].push({ path, handler });
26
32
  }
27
33
 
34
+ use(
35
+ middleware: (
36
+ req: IncomingMessage & { query?: any; body?: any },
37
+ res: ServerResponse,
38
+ next: () => void
39
+ ) => void
40
+ ): void {
41
+ this.middlewares.push(middleware);
42
+ }
43
+
28
44
  get(path: string, handler: Handler): void {
29
45
  this.addRoute("GET", path, handler);
30
46
  }
@@ -50,16 +66,26 @@ export default class ExpressPlus {
50
66
  const method = req.method as Method;
51
67
  const parsedUrl = parse(req.url || "", true);
52
68
  const pathname = parsedUrl.pathname || "/";
53
-
54
69
  const route = this.routes[method].find((r) => r.path === pathname);
55
70
 
56
- if (route) {
57
- (req as any).query = parsedUrl.query;
58
- route.handler(req as any, res);
59
- } else {
60
- res.statusCode = 404;
61
- res.end("Not Found");
62
- }
71
+ (req as any).query = parsedUrl.query;
72
+ (req as any).body = {};
73
+
74
+ let i = 0;
75
+
76
+ const next = () => {
77
+ const middleware = this.middlewares[i++];
78
+ if (middleware) {
79
+ middleware(req as any, res, next);
80
+ } else if (route) {
81
+ route.handler(req as any, res);
82
+ } else {
83
+ res.statusCode = 404;
84
+ res.end("Not Found");
85
+ }
86
+ };
87
+
88
+ next();
63
89
  });
64
90
 
65
91
  server.listen(port, callback);
package/src/test.ts CHANGED
@@ -1,31 +1,82 @@
1
1
  import ExpressPlus from "./index";
2
+ import { sum, sub, mul, div } from "@gieo/utils";
3
+
2
4
  const app = new ExpressPlus();
3
5
 
6
+ // GET /
4
7
  app.get("/", (req, res) => {
5
8
  res.end("Hello, World!");
6
9
  });
10
+
11
+ // GET /hello?name=...
7
12
  app.get("/hello", (req, res) => {
8
13
  const name = req.query.name || "Guest";
9
14
  res.end(`Hello, ${name}!`);
10
15
  });
11
- app.post("/data", (req, res) => {
16
+
17
+ // Middleware để parse body JSON
18
+ app.use((req, res, next) => {
12
19
  let body = "";
13
20
  req.on("data", (chunk) => {
14
21
  body += chunk.toString();
15
22
  });
16
23
  req.on("end", () => {
17
- res.end(`Received data: ${body}`);
24
+ try {
25
+ req.body = JSON.parse(body);
26
+ } catch (e) {
27
+ req.body = {};
28
+ }
29
+ next();
18
30
  });
19
31
  });
32
+
33
+ // POST /calculate
34
+ app.post("/calculate", (req, res) => {
35
+ const { a, b, op } = req.body;
36
+
37
+ if (typeof a !== "number" || typeof b !== "number") {
38
+ res.statusCode = 400;
39
+ return res.end("a và b phải là số");
40
+ }
41
+
42
+ let result: number;
43
+
44
+ switch (op) {
45
+ case "sum":
46
+ result = sum(a, b);
47
+ break;
48
+ case "sub":
49
+ result = sub(a, b);
50
+ break;
51
+ case "mul":
52
+ result = mul(a, b);
53
+ break;
54
+ case "div":
55
+ result = div(a, b);
56
+ break;
57
+ default:
58
+ res.statusCode = 400;
59
+ return res.end("Toán tử không hợp lệ (sum, sub, mul, div)");
60
+ }
61
+
62
+ res.setHeader("Content-Type", "application/json");
63
+ res.end(JSON.stringify({ result }));
64
+ });
65
+
66
+ // Các route khác
20
67
  app.put("/update", (req, res) => {
21
68
  res.end("Update successful");
22
69
  });
70
+
23
71
  app.patch("/modify", (req, res) => {
24
72
  res.end("Modify successful");
25
73
  });
74
+
26
75
  app.delete("/delete", (req, res) => {
27
76
  res.end("Delete successful");
28
77
  });
78
+
79
+ // Start server
29
80
  app.listen(3000, () => {
30
81
  console.log("Server is running on http://localhost:3000");
31
82
  });