diff --git a/examples/CRUD/router/crud.ts b/examples/CRUD/router/crud.ts index 3adba12..e5b99f6 100644 --- a/examples/CRUD/router/crud.ts +++ b/examples/CRUD/router/crud.ts @@ -2,17 +2,26 @@ import { exposed } from '../../../lib/core/decorators/exposed'; import { Delete, Get, Post, Put, Route } from '../../../lib/core/router'; import { Context } from '../../../lib/types/types'; import db from '../models/db.json'; +import fs from 'fs'; +import p from 'path'; +const file = p.join(__dirname, '..', 'models', 'db.json'); @Route('/') @exposed -class GetAllUser { +class CRUD { @Get() get(ctx: Context) { const users = JSON.stringify(db); - ctx.write(users); - ctx.end(); + ctx.writeHead(200, { + 'Content-Length': Buffer.byteLength(users), + 'Content-Type': 'text/plain', + }); + ctx.end(users); } @Post() - post(ctx: Context) {} + post(ctx: Context) { + const user = ctx.body + console.log('use:', user); + } @Delete() del(ctx: Context) {} @Put() diff --git a/lib/core/router/request.ts b/lib/core/router/request.ts index 47172b0..f7ac3d6 100644 --- a/lib/core/router/request.ts +++ b/lib/core/router/request.ts @@ -1,13 +1,23 @@ import { IncomingMessage } from 'http'; +import { logger } from '../../helper/logger'; const PROTO = IncomingMessage.prototype; -PROTO.json = function (): Promise { +PROTO.json = function (): Promise { return new Promise((resolve, reject) => { let body = ''; this.on('data', (chunk) => { body += chunk; }); this.on('end', () => { - resolve(JSON.parse(body)); + if (!body) { + logger.warn('Request body is empty.'); + resolve(undefined); + } else { + try { + resolve(JSON.parse(body)); + } catch (error) { + reject(new Error('Invalid JSON format.')); + } + } }); this.on('error', (error) => { reject(error); diff --git a/lib/core/server.ts b/lib/core/server.ts index 2b64b59..fe79bf2 100644 --- a/lib/core/server.ts +++ b/lib/core/server.ts @@ -45,7 +45,6 @@ export class WebServer extends Server implements Gland.Listener, Gland.APP { } else { throw new Error('Invalid middleware/handler function signature'); } - console.log('middlewares', this.middlewares); }); } else { // If the first argument is not a string, treat it as a middleware @@ -80,14 +79,8 @@ export class WebServer extends Server implements Gland.Listener, Gland.APP { ctx.params = params; // Check if the controller is a class or a function if (typeof controller === 'function' && !handlerKey) { - this.middlewares.forEach((fn, index) => { - console.log(`Middleware ${index + 1}:`, fn.name || fn.toString()); - }); const middlewareStack = Array.from(new Set([...this.middlewares, controller])); // Logging the function names or sources to help identify duplication - // middlewareStack.forEach((fn, index) => { - // console.log(`Middleware ${index + 1}:`, fn.name || fn.toString()); - // }); await Router.execute(ctx, middlewareStack); } else { const routeInstance = new controller(); diff --git a/lib/types/http.d.ts b/lib/types/http.d.ts index c605365..5b2e994 100644 --- a/lib/types/http.d.ts +++ b/lib/types/http.d.ts @@ -2,7 +2,7 @@ import { IncomingMessage, ServerResponse } from 'http'; declare module 'http' { interface IncomingMessage { - json(): Promise; + json(): Promise; get(header: string): string | undefined; path(): string; hostname(): string;