From e6f6b1ddc5c8aa76d011b934a445820203ab77d5 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Fri, 12 May 2023 14:10:41 +0200 Subject: [PATCH] add var to identify static resource requests --- example/test.ts | 8 +++++++- mod.ts | 13 ++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/example/test.ts b/example/test.ts index df392f4..1068a10 100644 --- a/example/test.ts +++ b/example/test.ts @@ -23,7 +23,13 @@ httpServer.preprocessor((_req, rep) => { httpServer.middleware(async (req, _rep, done) => { const processTime = await done(); - console.log(`${req.method} - ${req.remoteIpAddr} - ${req.path} - ${prettyTime(processTime)}`); + if (!req.resourceRequest) { + console.log( + `${req.method} - ${req.remoteIpAddr} - ${req.path} - ${ + prettyTime(processTime) + }`, + ); + } }); httpServer.error((req, _rep) => { diff --git a/mod.ts b/mod.ts index 0c7252a..de9fc02 100644 --- a/mod.ts +++ b/mod.ts @@ -122,6 +122,7 @@ export class HTTPServer { conn, filepath, url, + this.staticServePath ?? "", ); const routeReply: RouteReply = new RouteReply(); @@ -347,13 +348,23 @@ export class RouteRequest { queryParams: { [key: string]: string }; pathParams: { [key: string]: string }; remoteIpAddr: string; + resourceRequest: boolean; - constructor(request: Request, conn: Deno.Conn, path: string, url: string) { + constructor( + request: Request, + conn: Deno.Conn, + path: string, + url: string, + staticServePath: string, + ) { this.url = url; this.path = decodeURIComponent(path); this.headers = request.headers; this.method = request.method as HTTPMethod; this.pathParams = {}; + this.resourceRequest = staticServePath.length > 0 + ? path.startsWith(staticServePath) + : false; this.queryParams = Object.fromEntries(new URL(url).searchParams.entries()); this.remoteIpAddr = "hostname" in conn.remoteAddr ? conn.remoteAddr["hostname"]