@gieo/express 1.0.4 → 1.0.5
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/package.json +1 -1
- package/src/index.js +89 -0
- package/src/index.ts +0 -2
- package/src/test.js +106 -0
- package/tsconfig.json +6 -4
package/package.json
CHANGED
package/src/index.js
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const http_1 = require("http");
|
4
|
+
const url_1 = require("url");
|
5
|
+
class ExpressPlus {
|
6
|
+
constructor() {
|
7
|
+
this.routes = {
|
8
|
+
GET: [],
|
9
|
+
POST: [],
|
10
|
+
PUT: [],
|
11
|
+
PATCH: [],
|
12
|
+
DELETE: [],
|
13
|
+
};
|
14
|
+
this.middlewares = [];
|
15
|
+
}
|
16
|
+
addRoute(method, path, handler) {
|
17
|
+
this.routes[method].push({ path, handler });
|
18
|
+
}
|
19
|
+
use(middleware) {
|
20
|
+
this.middlewares.push(middleware);
|
21
|
+
}
|
22
|
+
get(path, handler) {
|
23
|
+
this.addRoute("GET", path, handler);
|
24
|
+
}
|
25
|
+
post(path, handler) {
|
26
|
+
this.addRoute("POST", path, handler);
|
27
|
+
}
|
28
|
+
put(path, handler) {
|
29
|
+
this.addRoute("PUT", path, handler);
|
30
|
+
}
|
31
|
+
patch(path, handler) {
|
32
|
+
this.addRoute("PATCH", path, handler);
|
33
|
+
}
|
34
|
+
delete(path, handler) {
|
35
|
+
this.addRoute("DELETE", path, handler);
|
36
|
+
}
|
37
|
+
listen(port, callback) {
|
38
|
+
const server = (0, http_1.createServer)((req, res) => {
|
39
|
+
const method = req.method;
|
40
|
+
const parsedUrl = (0, url_1.parse)(req.url || "", true);
|
41
|
+
const pathname = parsedUrl.pathname || "/";
|
42
|
+
const route = this.routes[method].find((r) => r.path === pathname);
|
43
|
+
const extendedReq = req;
|
44
|
+
const extendedRes = res;
|
45
|
+
extendedReq.query = parsedUrl.query;
|
46
|
+
extendedReq.getBody = async () => {
|
47
|
+
if (extendedReq.body)
|
48
|
+
return extendedReq.body;
|
49
|
+
const chunks = [];
|
50
|
+
for await (const chunk of extendedReq) {
|
51
|
+
chunks.push(chunk);
|
52
|
+
}
|
53
|
+
const raw = Buffer.concat(chunks).toString();
|
54
|
+
extendedReq.bodyRaw = raw;
|
55
|
+
try {
|
56
|
+
extendedReq.body = JSON.parse(raw);
|
57
|
+
}
|
58
|
+
catch {
|
59
|
+
extendedReq.body = {};
|
60
|
+
}
|
61
|
+
return extendedReq.body;
|
62
|
+
};
|
63
|
+
extendedRes.status = function (code) {
|
64
|
+
extendedRes.statusCode = code;
|
65
|
+
return extendedRes;
|
66
|
+
};
|
67
|
+
extendedRes.json = function (data) {
|
68
|
+
extendedRes.setHeader("Content-Type", "application/json");
|
69
|
+
extendedRes.end(JSON.stringify(data));
|
70
|
+
};
|
71
|
+
let i = 0;
|
72
|
+
const next = () => {
|
73
|
+
const middleware = this.middlewares[i++];
|
74
|
+
if (middleware) {
|
75
|
+
middleware(extendedReq, extendedRes, next);
|
76
|
+
}
|
77
|
+
else if (route) {
|
78
|
+
route.handler(extendedReq, extendedRes);
|
79
|
+
}
|
80
|
+
else {
|
81
|
+
extendedRes.status(404).end("Not Found");
|
82
|
+
}
|
83
|
+
};
|
84
|
+
next();
|
85
|
+
});
|
86
|
+
server.listen(port, callback);
|
87
|
+
}
|
88
|
+
}
|
89
|
+
exports.default = ExpressPlus;
|
package/src/index.ts
CHANGED
package/src/test.js
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const index_1 = __importDefault(require("./index"));
|
7
|
+
const utils_1 = require("@gieo/utils");
|
8
|
+
const app = new index_1.default();
|
9
|
+
app.post("/calc", async (req, res) => {
|
10
|
+
const { op, ...numbers } = await req.getBody();
|
11
|
+
// Lấy tất cả giá trị số từ body
|
12
|
+
const values = Object.values(numbers)
|
13
|
+
.map(Number)
|
14
|
+
.filter((v) => !isNaN(v));
|
15
|
+
if (values.length < 2) {
|
16
|
+
return res.status(400).json({
|
17
|
+
statusCode: 400,
|
18
|
+
success: false,
|
19
|
+
message: "Need at least two numeric values",
|
20
|
+
});
|
21
|
+
}
|
22
|
+
let result;
|
23
|
+
try {
|
24
|
+
switch (op) {
|
25
|
+
case "sum":
|
26
|
+
result = (0, utils_1.sum)(...values);
|
27
|
+
break;
|
28
|
+
case "sub":
|
29
|
+
result = (0, utils_1.sub)(...values);
|
30
|
+
break;
|
31
|
+
case "mul":
|
32
|
+
result = (0, utils_1.mul)(...values);
|
33
|
+
break;
|
34
|
+
case "div":
|
35
|
+
result = (0, utils_1.div)(...values);
|
36
|
+
break;
|
37
|
+
default:
|
38
|
+
return res.status(400).json({
|
39
|
+
statusCode: 400,
|
40
|
+
success: false,
|
41
|
+
message: "Invalid operator",
|
42
|
+
});
|
43
|
+
}
|
44
|
+
return res.status(200).json({
|
45
|
+
statusCode: 200,
|
46
|
+
success: true,
|
47
|
+
result,
|
48
|
+
});
|
49
|
+
}
|
50
|
+
catch (err) {
|
51
|
+
return res.status(500).json({
|
52
|
+
statusCode: 500,
|
53
|
+
success: false,
|
54
|
+
message: err.message,
|
55
|
+
});
|
56
|
+
}
|
57
|
+
});
|
58
|
+
// Các route cơ bản
|
59
|
+
app.get("/", (req, res) => res.status(200).json({ success: true, status: 200, result: "GET / OK" }));
|
60
|
+
app.put("/", (req, res) => res.status(200).json({ success: true, status: 200, result: "PUT / OK" }));
|
61
|
+
app.post("/", (req, res) => res.status(200).json({ success: true, status: 200, result: "POST / OK" }));
|
62
|
+
app.patch("/", (req, res) => res.status(200).json({ success: true, status: 200, result: "PATCH / OK" }));
|
63
|
+
app.delete("/", (req, res) => res.status(200).json({ success: true, status: 200, result: "DELETE / OK" }));
|
64
|
+
const mathRoute = (op) => async (req, res) => {
|
65
|
+
const { a, b } = await req.getBody();
|
66
|
+
if (typeof a !== "number" || typeof b !== "number") {
|
67
|
+
return res.status(400).json({
|
68
|
+
success: false,
|
69
|
+
status: 400,
|
70
|
+
result: "Invalid input. 'a' and 'b' must be numbers.",
|
71
|
+
});
|
72
|
+
}
|
73
|
+
try {
|
74
|
+
const result = op(a, b);
|
75
|
+
res.status(200).json({ success: true, status: 200, result });
|
76
|
+
}
|
77
|
+
catch (err) {
|
78
|
+
res
|
79
|
+
.status(500)
|
80
|
+
.json({ success: false, status: 500, result: err.message });
|
81
|
+
}
|
82
|
+
};
|
83
|
+
// Math operations
|
84
|
+
app.post("/sum", mathRoute(utils_1.sum));
|
85
|
+
app.post("/sub", mathRoute(utils_1.sub));
|
86
|
+
app.post("/mul", mathRoute(utils_1.mul));
|
87
|
+
app.post("/div", async (req, res) => {
|
88
|
+
const { a, b } = await req.getBody();
|
89
|
+
if (typeof a !== "number" || typeof b !== "number") {
|
90
|
+
return res.status(400).json({
|
91
|
+
success: false,
|
92
|
+
status: 400,
|
93
|
+
result: "Invalid input. 'a' and 'b' must be numbers.",
|
94
|
+
});
|
95
|
+
}
|
96
|
+
if (b === 0) {
|
97
|
+
return res
|
98
|
+
.status(400)
|
99
|
+
.json({ success: false, status: 400, result: "Cannot divide by zero" });
|
100
|
+
}
|
101
|
+
const result = (0, utils_1.div)(a, b);
|
102
|
+
res.status(200).json({ success: true, status: 200, result });
|
103
|
+
});
|
104
|
+
app.listen(3000, () => {
|
105
|
+
console.log("✅ Server is running at http://localhost:3000");
|
106
|
+
});
|
package/tsconfig.json
CHANGED