@gieo/express 1.0.0 → 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 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");
@@ -11,10 +54,14 @@ var ExpressPlus = /** @class */ (function () {
11
54
  PATCH: [],
12
55
  DELETE: [],
13
56
  };
57
+ this.middlewares = [];
14
58
  }
15
59
  ExpressPlus.prototype.addRoute = function (method, path, handler) {
16
60
  this.routes[method].push({ path: path, handler: handler });
17
61
  };
62
+ ExpressPlus.prototype.use = function (middleware) {
63
+ this.middlewares.push(middleware);
64
+ };
18
65
  ExpressPlus.prototype.get = function (path, handler) {
19
66
  this.addRoute("GET", path, handler);
20
67
  };
@@ -37,14 +84,86 @@ var ExpressPlus = /** @class */ (function () {
37
84
  var parsedUrl = (0, url_1.parse)(req.url || "", true);
38
85
  var pathname = parsedUrl.pathname || "/";
39
86
  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
- }
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
+ };
153
+ var i = 0;
154
+ var next = function () {
155
+ var middleware = _this.middlewares[i++];
156
+ if (middleware) {
157
+ middleware(extendedReq, extendedRes, next);
158
+ }
159
+ else if (route) {
160
+ route.handler(extendedReq, extendedRes);
161
+ }
162
+ else {
163
+ extendedRes.status(404).end("Not Found");
164
+ }
165
+ };
166
+ next();
48
167
  });
49
168
  server.listen(port, callback);
50
169
  };
package/dist/test.js CHANGED
@@ -1,32 +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");
40
+ var utils_1 = require("@gieo/utils");
4
41
  var app = new index_1.default();
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
+ })];
74
+ }
75
+ });
76
+ }); });
77
+ // Các route cơ bản
5
78
  app.get("/", function (req, res) {
6
- res.end("Hello, World!");
79
+ return res.status(200).json({ success: true, status: 200, result: "GET / OK" });
7
80
  });
8
- app.get("/hello", function (req, res) {
9
- var name = req.query.name || "Guest";
10
- res.end("Hello, ".concat(name, "!"));
81
+ app.put("/", function (req, res) {
82
+ return res.status(200).json({ success: true, status: 200, result: "PUT / OK" });
11
83
  });
12
- app.post("/data", function (req, res) {
13
- var body = "";
14
- req.on("data", function (chunk) {
15
- body += chunk.toString();
16
- });
17
- req.on("end", function () {
18
- res.end("Received data: ".concat(body));
19
- });
84
+ app.post("/", function (req, res) {
85
+ return res.status(200).json({ success: true, status: 200, result: "POST / OK" });
20
86
  });
21
- app.put("/update", function (req, res) {
22
- res.end("Update successful");
87
+ app.patch("/", function (req, res) {
88
+ return res.status(200).json({ success: true, status: 200, result: "PATCH / OK" });
23
89
  });
24
- app.patch("/modify", function (req, res) {
25
- res.end("Modify successful");
26
- });
27
- app.delete("/delete", function (req, res) {
28
- res.end("Delete successful");
90
+ app.delete("/", function (req, res) {
91
+ return res.status(200).json({ success: true, status: 200, result: "DELETE / OK" });
29
92
  });
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
+ }); });
30
149
  app.listen(3000, function () {
31
- console.log("Server is running on http://localhost:3000");
150
+ console.log("Server is running at http://localhost:3000");
32
151
  });
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@gieo/express",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
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
@@ -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 },
7
- res: ServerResponse
8
- ) => void;
19
+ type Handler = (req: ExtendedRequest, res: ExtendedResponse) => void;
9
20
 
10
21
  interface Route {
11
22
  path: string;
@@ -21,10 +32,26 @@ export default class ExpressPlus {
21
32
  DELETE: [],
22
33
  };
23
34
 
35
+ private middlewares: ((
36
+ req: ExtendedRequest,
37
+ res: ExtendedResponse,
38
+ next: () => void
39
+ ) => void)[] = [];
40
+
24
41
  private addRoute(method: Method, path: string, handler: Handler): void {
25
42
  this.routes[method].push({ path, handler });
26
43
  }
27
44
 
45
+ use(
46
+ middleware: (
47
+ req: ExtendedRequest,
48
+ res: ExtendedResponse,
49
+ next: () => void
50
+ ) => void
51
+ ): void {
52
+ this.middlewares.push(middleware);
53
+ }
54
+
28
55
  get(path: string, handler: Handler): void {
29
56
  this.addRoute("GET", path, handler);
30
57
  }
@@ -53,13 +80,53 @@ export default class ExpressPlus {
53
80
 
54
81
  const route = this.routes[method].find((r) => r.path === pathname);
55
82
 
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
- }
83
+ const extendedReq = req as ExtendedRequest;
84
+ const extendedRes = res as ExtendedResponse;
85
+
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;
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;
118
+ const next = () => {
119
+ const middleware = this.middlewares[i++];
120
+ if (middleware) {
121
+ middleware(extendedReq, extendedRes, next);
122
+ } else if (route) {
123
+ route.handler(extendedReq, extendedRes);
124
+ } else {
125
+ extendedRes.status(404).end("Not Found");
126
+ }
127
+ };
128
+
129
+ next();
63
130
  });
64
131
 
65
132
  server.listen(port, callback);
package/src/test.ts CHANGED
@@ -1,31 +1,98 @@
1
1
  import ExpressPlus from "./index";
2
+ import { sum, sub, mul, div } from "@gieo/utils";
2
3
  const app = new ExpressPlus();
3
4
 
4
- app.get("/", (req, res) => {
5
- res.end("Hello, World!");
6
- });
7
- app.get("/hello", (req, res) => {
8
- const name = req.query.name || "Guest";
9
- res.end(`Hello, ${name}!`);
10
- });
11
- app.post("/data", (req, res) => {
12
- let body = "";
13
- req.on("data", (chunk) => {
14
- body += chunk.toString();
15
- });
16
- req.on("end", () => {
17
- res.end(`Received data: ${body}`);
5
+ app.post("/calc", async (req, res) => {
6
+ const { a, b, op } = await req.getBody();
7
+
8
+ let result: number;
9
+ switch (op) {
10
+ case "sum":
11
+ result = sum(a, b);
12
+ break;
13
+ case "sub":
14
+ result = sub(a, b);
15
+ break;
16
+ case "mul":
17
+ result = mul(a, b);
18
+ break;
19
+ case "div":
20
+ result = div(a, b);
21
+ break;
22
+ default:
23
+ return res.status(400).json({
24
+ statusCode: 400,
25
+ success: false,
26
+ message: "Invalid operator",
27
+ });
28
+ }
29
+
30
+ return res.status(200).json({
31
+ statusCode: 200,
32
+ success: true,
33
+ result,
18
34
  });
19
35
  });
20
- app.put("/update", (req, res) => {
21
- res.end("Update successful");
22
- });
23
- app.patch("/modify", (req, res) => {
24
- res.end("Modify successful");
25
- });
26
- app.delete("/delete", (req, res) => {
27
- res.end("Delete successful");
36
+
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
+ );
53
+
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
+ }
64
+
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
+ };
74
+
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 });
28
95
  });
29
96
  app.listen(3000, () => {
30
- console.log("Server is running on http://localhost:3000");
97
+ console.log("Server is running at http://localhost:3000");
31
98
  });