@gieo/express 1.0.1 → 1.0.2
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 +112 -6
- package/dist/test.js +137 -58
- package/package.json +1 -1
- package/src/index.ts +56 -15
- package/src/test.ts +69 -53
package/dist/index.js
CHANGED
@@ -1,4 +1,47 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
15
|
+
function step(op) {
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
20
|
+
switch (op[0]) {
|
21
|
+
case 0: case 1: t = op; break;
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25
|
+
default:
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30
|
+
if (t[2]) _.ops.pop();
|
31
|
+
_.trys.pop(); continue;
|
32
|
+
}
|
33
|
+
op = body.call(thisArg, _);
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36
|
+
}
|
37
|
+
};
|
38
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
39
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
40
|
+
var m = o[Symbol.asyncIterator], i;
|
41
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
42
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
43
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
44
|
+
};
|
2
45
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
46
|
var http_1 = require("http");
|
4
47
|
var url_1 = require("url");
|
@@ -41,20 +84,83 @@ var ExpressPlus = /** @class */ (function () {
|
|
41
84
|
var parsedUrl = (0, url_1.parse)(req.url || "", true);
|
42
85
|
var pathname = parsedUrl.pathname || "/";
|
43
86
|
var route = _this.routes[method].find(function (r) { return r.path === pathname; });
|
44
|
-
|
45
|
-
|
87
|
+
var extendedReq = req;
|
88
|
+
var extendedRes = res;
|
89
|
+
extendedReq.query = parsedUrl.query;
|
90
|
+
extendedReq.getBody = function () { return __awaiter(_this, void 0, void 0, function () {
|
91
|
+
var chunks, _a, extendedReq_1, extendedReq_1_1, chunk, e_1_1, raw;
|
92
|
+
var _b, e_1, _c, _d;
|
93
|
+
return __generator(this, function (_e) {
|
94
|
+
switch (_e.label) {
|
95
|
+
case 0:
|
96
|
+
if (extendedReq.body)
|
97
|
+
return [2 /*return*/, extendedReq.body];
|
98
|
+
chunks = [];
|
99
|
+
_e.label = 1;
|
100
|
+
case 1:
|
101
|
+
_e.trys.push([1, 6, 7, 12]);
|
102
|
+
_a = true, extendedReq_1 = __asyncValues(extendedReq);
|
103
|
+
_e.label = 2;
|
104
|
+
case 2: return [4 /*yield*/, extendedReq_1.next()];
|
105
|
+
case 3:
|
106
|
+
if (!(extendedReq_1_1 = _e.sent(), _b = extendedReq_1_1.done, !_b)) return [3 /*break*/, 5];
|
107
|
+
_d = extendedReq_1_1.value;
|
108
|
+
_a = false;
|
109
|
+
chunk = _d;
|
110
|
+
chunks.push(chunk);
|
111
|
+
_e.label = 4;
|
112
|
+
case 4:
|
113
|
+
_a = true;
|
114
|
+
return [3 /*break*/, 2];
|
115
|
+
case 5: return [3 /*break*/, 12];
|
116
|
+
case 6:
|
117
|
+
e_1_1 = _e.sent();
|
118
|
+
e_1 = { error: e_1_1 };
|
119
|
+
return [3 /*break*/, 12];
|
120
|
+
case 7:
|
121
|
+
_e.trys.push([7, , 10, 11]);
|
122
|
+
if (!(!_a && !_b && (_c = extendedReq_1.return))) return [3 /*break*/, 9];
|
123
|
+
return [4 /*yield*/, _c.call(extendedReq_1)];
|
124
|
+
case 8:
|
125
|
+
_e.sent();
|
126
|
+
_e.label = 9;
|
127
|
+
case 9: return [3 /*break*/, 11];
|
128
|
+
case 10:
|
129
|
+
if (e_1) throw e_1.error;
|
130
|
+
return [7 /*endfinally*/];
|
131
|
+
case 11: return [7 /*endfinally*/];
|
132
|
+
case 12:
|
133
|
+
raw = Buffer.concat(chunks).toString();
|
134
|
+
extendedReq.bodyRaw = raw;
|
135
|
+
try {
|
136
|
+
extendedReq.body = JSON.parse(raw);
|
137
|
+
}
|
138
|
+
catch (_f) {
|
139
|
+
extendedReq.body = {};
|
140
|
+
}
|
141
|
+
return [2 /*return*/, extendedReq.body];
|
142
|
+
}
|
143
|
+
});
|
144
|
+
}); };
|
145
|
+
extendedRes.status = function (code) {
|
146
|
+
extendedRes.statusCode = code;
|
147
|
+
return extendedRes;
|
148
|
+
};
|
149
|
+
extendedRes.json = function (data) {
|
150
|
+
extendedRes.setHeader("Content-Type", "application/json");
|
151
|
+
extendedRes.end(JSON.stringify(data));
|
152
|
+
};
|
46
153
|
var i = 0;
|
47
154
|
var next = function () {
|
48
155
|
var middleware = _this.middlewares[i++];
|
49
156
|
if (middleware) {
|
50
|
-
middleware(
|
157
|
+
middleware(extendedReq, extendedRes, next);
|
51
158
|
}
|
52
159
|
else if (route) {
|
53
|
-
route.handler(
|
160
|
+
route.handler(extendedReq, extendedRes);
|
54
161
|
}
|
55
162
|
else {
|
56
|
-
|
57
|
-
res.end("Not Found");
|
163
|
+
extendedRes.status(404).end("Not Found");
|
58
164
|
}
|
59
165
|
};
|
60
166
|
next();
|
package/dist/test.js
CHANGED
@@ -1,72 +1,151 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
15
|
+
function step(op) {
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
20
|
+
switch (op[0]) {
|
21
|
+
case 0: case 1: t = op; break;
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25
|
+
default:
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30
|
+
if (t[2]) _.ops.pop();
|
31
|
+
_.trys.pop(); continue;
|
32
|
+
}
|
33
|
+
op = body.call(thisArg, _);
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36
|
+
}
|
37
|
+
};
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
39
|
var index_1 = require("./index");
|
4
40
|
var utils_1 = require("@gieo/utils");
|
5
41
|
var app = new index_1.default();
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
42
|
+
app.post("/calc", function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
43
|
+
var _a, a, b, op, result;
|
44
|
+
return __generator(this, function (_b) {
|
45
|
+
switch (_b.label) {
|
46
|
+
case 0: return [4 /*yield*/, req.getBody()];
|
47
|
+
case 1:
|
48
|
+
_a = _b.sent(), a = _a.a, b = _a.b, op = _a.op;
|
49
|
+
switch (op) {
|
50
|
+
case "sum":
|
51
|
+
result = (0, utils_1.sum)(a, b);
|
52
|
+
break;
|
53
|
+
case "sub":
|
54
|
+
result = (0, utils_1.sub)(a, b);
|
55
|
+
break;
|
56
|
+
case "mul":
|
57
|
+
result = (0, utils_1.mul)(a, b);
|
58
|
+
break;
|
59
|
+
case "div":
|
60
|
+
result = (0, utils_1.div)(a, b);
|
61
|
+
break;
|
62
|
+
default:
|
63
|
+
return [2 /*return*/, res.status(400).json({
|
64
|
+
statusCode: 400,
|
65
|
+
success: false,
|
66
|
+
message: "Invalid operator",
|
67
|
+
})];
|
68
|
+
}
|
69
|
+
return [2 /*return*/, res.status(200).json({
|
70
|
+
statusCode: 200,
|
71
|
+
success: true,
|
72
|
+
result: result,
|
73
|
+
})];
|
27
74
|
}
|
28
|
-
next();
|
29
75
|
});
|
76
|
+
}); });
|
77
|
+
// Các route cơ bản
|
78
|
+
app.get("/", function (req, res) {
|
79
|
+
return res.status(200).json({ success: true, status: 200, result: "GET / OK" });
|
30
80
|
});
|
31
|
-
|
32
|
-
|
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 }));
|
81
|
+
app.put("/", function (req, res) {
|
82
|
+
return res.status(200).json({ success: true, status: 200, result: "PUT / OK" });
|
58
83
|
});
|
59
|
-
|
60
|
-
|
61
|
-
res.end("Update successful");
|
84
|
+
app.post("/", function (req, res) {
|
85
|
+
return res.status(200).json({ success: true, status: 200, result: "POST / OK" });
|
62
86
|
});
|
63
|
-
app.patch("/
|
64
|
-
res.
|
87
|
+
app.patch("/", function (req, res) {
|
88
|
+
return res.status(200).json({ success: true, status: 200, result: "PATCH / OK" });
|
65
89
|
});
|
66
|
-
app.delete("/
|
67
|
-
res.
|
90
|
+
app.delete("/", function (req, res) {
|
91
|
+
return res.status(200).json({ success: true, status: 200, result: "DELETE / OK" });
|
68
92
|
});
|
69
|
-
|
93
|
+
var mathRoute = function (op) { return function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
94
|
+
var _a, a, b, result;
|
95
|
+
return __generator(this, function (_b) {
|
96
|
+
switch (_b.label) {
|
97
|
+
case 0: return [4 /*yield*/, req.getBody()];
|
98
|
+
case 1:
|
99
|
+
_a = _b.sent(), a = _a.a, b = _a.b;
|
100
|
+
if (typeof a !== "number" || typeof b !== "number") {
|
101
|
+
return [2 /*return*/, res.status(400).json({
|
102
|
+
success: false,
|
103
|
+
status: 400,
|
104
|
+
result: "Invalid input. 'a' and 'b' must be numbers.",
|
105
|
+
})];
|
106
|
+
}
|
107
|
+
try {
|
108
|
+
result = op(a, b);
|
109
|
+
res.status(200).json({ success: true, status: 200, result: result });
|
110
|
+
}
|
111
|
+
catch (err) {
|
112
|
+
res
|
113
|
+
.status(500)
|
114
|
+
.json({ success: false, status: 500, result: err.message });
|
115
|
+
}
|
116
|
+
return [2 /*return*/];
|
117
|
+
}
|
118
|
+
});
|
119
|
+
}); }; };
|
120
|
+
// Math operations
|
121
|
+
app.post("/sum", mathRoute(utils_1.sum));
|
122
|
+
app.post("/sub", mathRoute(utils_1.sub));
|
123
|
+
app.post("/mul", mathRoute(utils_1.mul));
|
124
|
+
app.post("/div", function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
125
|
+
var _a, a, b, result;
|
126
|
+
return __generator(this, function (_b) {
|
127
|
+
switch (_b.label) {
|
128
|
+
case 0: return [4 /*yield*/, req.getBody()];
|
129
|
+
case 1:
|
130
|
+
_a = _b.sent(), a = _a.a, b = _a.b;
|
131
|
+
if (typeof a !== "number" || typeof b !== "number") {
|
132
|
+
return [2 /*return*/, res.status(400).json({
|
133
|
+
success: false,
|
134
|
+
status: 400,
|
135
|
+
result: "Invalid input. 'a' and 'b' must be numbers.",
|
136
|
+
})];
|
137
|
+
}
|
138
|
+
if (b === 0) {
|
139
|
+
return [2 /*return*/, res
|
140
|
+
.status(400)
|
141
|
+
.json({ success: false, status: 400, result: "Cannot divide by zero" })];
|
142
|
+
}
|
143
|
+
result = (0, utils_1.div)(a, b);
|
144
|
+
res.status(200).json({ success: true, status: 200, result: result });
|
145
|
+
return [2 /*return*/];
|
146
|
+
}
|
147
|
+
});
|
148
|
+
}); });
|
70
149
|
app.listen(3000, function () {
|
71
|
-
console.log("Server is running
|
150
|
+
console.log("✅ Server is running at http://localhost:3000");
|
72
151
|
});
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
@@ -1,11 +1,22 @@
|
|
1
1
|
import { createServer, IncomingMessage, ServerResponse } from "http";
|
2
2
|
import { parse } from "url";
|
3
3
|
|
4
|
+
// Mở rộng Request
|
5
|
+
interface ExtendedRequest extends IncomingMessage {
|
6
|
+
query?: Record<string, string | string[]>;
|
7
|
+
bodyRaw?: string;
|
8
|
+
body?: any;
|
9
|
+
getBody: () => Promise<any>;
|
10
|
+
}
|
11
|
+
|
12
|
+
// Mở rộng Response
|
13
|
+
interface ExtendedResponse extends ServerResponse {
|
14
|
+
status: (code: number) => ExtendedResponse;
|
15
|
+
json: (data: any) => void;
|
16
|
+
}
|
17
|
+
|
4
18
|
type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
5
|
-
type Handler = (
|
6
|
-
req: IncomingMessage & { query?: any; body?: any },
|
7
|
-
res: ServerResponse
|
8
|
-
) => void;
|
19
|
+
type Handler = (req: ExtendedRequest, res: ExtendedResponse) => void;
|
9
20
|
|
10
21
|
interface Route {
|
11
22
|
path: string;
|
@@ -22,8 +33,8 @@ export default class ExpressPlus {
|
|
22
33
|
};
|
23
34
|
|
24
35
|
private middlewares: ((
|
25
|
-
req:
|
26
|
-
res:
|
36
|
+
req: ExtendedRequest,
|
37
|
+
res: ExtendedResponse,
|
27
38
|
next: () => void
|
28
39
|
) => void)[] = [];
|
29
40
|
|
@@ -33,8 +44,8 @@ export default class ExpressPlus {
|
|
33
44
|
|
34
45
|
use(
|
35
46
|
middleware: (
|
36
|
-
req:
|
37
|
-
res:
|
47
|
+
req: ExtendedRequest,
|
48
|
+
res: ExtendedResponse,
|
38
49
|
next: () => void
|
39
50
|
) => void
|
40
51
|
): void {
|
@@ -66,22 +77,52 @@ export default class ExpressPlus {
|
|
66
77
|
const method = req.method as Method;
|
67
78
|
const parsedUrl = parse(req.url || "", true);
|
68
79
|
const pathname = parsedUrl.pathname || "/";
|
80
|
+
|
69
81
|
const route = this.routes[method].find((r) => r.path === pathname);
|
70
82
|
|
71
|
-
|
72
|
-
|
83
|
+
const extendedReq = req as ExtendedRequest;
|
84
|
+
const extendedRes = res as ExtendedResponse;
|
73
85
|
|
74
|
-
|
86
|
+
extendedReq.query = parsedUrl.query;
|
87
|
+
extendedReq.getBody = async () => {
|
88
|
+
if (extendedReq.body) return extendedReq.body;
|
89
|
+
|
90
|
+
const chunks: Buffer[] = [];
|
91
|
+
for await (const chunk of extendedReq) {
|
92
|
+
chunks.push(chunk);
|
93
|
+
}
|
94
|
+
|
95
|
+
const raw = Buffer.concat(chunks).toString();
|
96
|
+
extendedReq.bodyRaw = raw;
|
75
97
|
|
98
|
+
try {
|
99
|
+
extendedReq.body = JSON.parse(raw);
|
100
|
+
} catch {
|
101
|
+
extendedReq.body = {};
|
102
|
+
}
|
103
|
+
|
104
|
+
return extendedReq.body;
|
105
|
+
};
|
106
|
+
|
107
|
+
extendedRes.status = function (code: number) {
|
108
|
+
extendedRes.statusCode = code;
|
109
|
+
return extendedRes;
|
110
|
+
};
|
111
|
+
|
112
|
+
extendedRes.json = function (data: any) {
|
113
|
+
extendedRes.setHeader("Content-Type", "application/json");
|
114
|
+
extendedRes.end(JSON.stringify(data));
|
115
|
+
};
|
116
|
+
|
117
|
+
let i = 0;
|
76
118
|
const next = () => {
|
77
119
|
const middleware = this.middlewares[i++];
|
78
120
|
if (middleware) {
|
79
|
-
middleware(
|
121
|
+
middleware(extendedReq, extendedRes, next);
|
80
122
|
} else if (route) {
|
81
|
-
route.handler(
|
123
|
+
route.handler(extendedReq, extendedRes);
|
82
124
|
} else {
|
83
|
-
|
84
|
-
res.end("Not Found");
|
125
|
+
extendedRes.status(404).end("Not Found");
|
85
126
|
}
|
86
127
|
};
|
87
128
|
|
package/src/test.ts
CHANGED
@@ -1,46 +1,11 @@
|
|
1
1
|
import ExpressPlus from "./index";
|
2
2
|
import { sum, sub, mul, div } from "@gieo/utils";
|
3
|
-
|
4
3
|
const app = new ExpressPlus();
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
res.end("Hello, World!");
|
9
|
-
});
|
10
|
-
|
11
|
-
// GET /hello?name=...
|
12
|
-
app.get("/hello", (req, res) => {
|
13
|
-
const name = req.query.name || "Guest";
|
14
|
-
res.end(`Hello, ${name}!`);
|
15
|
-
});
|
16
|
-
|
17
|
-
// Middleware để parse body JSON
|
18
|
-
app.use((req, res, next) => {
|
19
|
-
let body = "";
|
20
|
-
req.on("data", (chunk) => {
|
21
|
-
body += chunk.toString();
|
22
|
-
});
|
23
|
-
req.on("end", () => {
|
24
|
-
try {
|
25
|
-
req.body = JSON.parse(body);
|
26
|
-
} catch (e) {
|
27
|
-
req.body = {};
|
28
|
-
}
|
29
|
-
next();
|
30
|
-
});
|
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
|
-
}
|
5
|
+
app.post("/calc", async (req, res) => {
|
6
|
+
const { a, b, op } = await req.getBody();
|
41
7
|
|
42
8
|
let result: number;
|
43
|
-
|
44
9
|
switch (op) {
|
45
10
|
case "sum":
|
46
11
|
result = sum(a, b);
|
@@ -55,28 +20,79 @@ app.post("/calculate", (req, res) => {
|
|
55
20
|
result = div(a, b);
|
56
21
|
break;
|
57
22
|
default:
|
58
|
-
res.
|
59
|
-
|
23
|
+
return res.status(400).json({
|
24
|
+
statusCode: 400,
|
25
|
+
success: false,
|
26
|
+
message: "Invalid operator",
|
27
|
+
});
|
60
28
|
}
|
61
29
|
|
62
|
-
res.
|
63
|
-
|
30
|
+
return res.status(200).json({
|
31
|
+
statusCode: 200,
|
32
|
+
success: true,
|
33
|
+
result,
|
34
|
+
});
|
64
35
|
});
|
65
36
|
|
66
|
-
// Các route
|
67
|
-
app.
|
68
|
-
res.
|
69
|
-
|
37
|
+
// Các route cơ bản
|
38
|
+
app.get("/", (req, res) =>
|
39
|
+
res.status(200).json({ success: true, status: 200, result: "GET / OK" })
|
40
|
+
);
|
41
|
+
app.put("/", (req, res) =>
|
42
|
+
res.status(200).json({ success: true, status: 200, result: "PUT / OK" })
|
43
|
+
);
|
44
|
+
app.post("/", (req, res) =>
|
45
|
+
res.status(200).json({ success: true, status: 200, result: "POST / OK" })
|
46
|
+
);
|
47
|
+
app.patch("/", (req, res) =>
|
48
|
+
res.status(200).json({ success: true, status: 200, result: "PATCH / OK" })
|
49
|
+
);
|
50
|
+
app.delete("/", (req, res) =>
|
51
|
+
res.status(200).json({ success: true, status: 200, result: "DELETE / OK" })
|
52
|
+
);
|
70
53
|
|
71
|
-
|
72
|
-
|
73
|
-
});
|
54
|
+
const mathRoute =
|
55
|
+
(op: (a: number, b: number) => number) => async (req: any, res: any) => {
|
56
|
+
const { a, b } = await req.getBody();
|
57
|
+
if (typeof a !== "number" || typeof b !== "number") {
|
58
|
+
return res.status(400).json({
|
59
|
+
success: false,
|
60
|
+
status: 400,
|
61
|
+
result: "Invalid input. 'a' and 'b' must be numbers.",
|
62
|
+
});
|
63
|
+
}
|
74
64
|
|
75
|
-
|
76
|
-
|
77
|
-
});
|
65
|
+
try {
|
66
|
+
const result = op(a, b);
|
67
|
+
res.status(200).json({ success: true, status: 200, result });
|
68
|
+
} catch (err: any) {
|
69
|
+
res
|
70
|
+
.status(500)
|
71
|
+
.json({ success: false, status: 500, result: err.message });
|
72
|
+
}
|
73
|
+
};
|
78
74
|
|
79
|
-
//
|
75
|
+
// Math operations
|
76
|
+
app.post("/sum", mathRoute(sum));
|
77
|
+
app.post("/sub", mathRoute(sub));
|
78
|
+
app.post("/mul", mathRoute(mul));
|
79
|
+
app.post("/div", async (req, res) => {
|
80
|
+
const { a, b } = await req.getBody();
|
81
|
+
if (typeof a !== "number" || typeof b !== "number") {
|
82
|
+
return res.status(400).json({
|
83
|
+
success: false,
|
84
|
+
status: 400,
|
85
|
+
result: "Invalid input. 'a' and 'b' must be numbers.",
|
86
|
+
});
|
87
|
+
}
|
88
|
+
if (b === 0) {
|
89
|
+
return res
|
90
|
+
.status(400)
|
91
|
+
.json({ success: false, status: 400, result: "Cannot divide by zero" });
|
92
|
+
}
|
93
|
+
const result = div(a, b);
|
94
|
+
res.status(200).json({ success: true, status: 200, result });
|
95
|
+
});
|
80
96
|
app.listen(3000, () => {
|
81
|
-
console.log("Server is running
|
97
|
+
console.log("✅ Server is running at http://localhost:3000");
|
82
98
|
});
|