From c8fb3bfd903905c32f85fa7b491697806eb87e86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Feli=CE=9E?= Date: Tue, 5 Jul 2022 08:50:46 +0000 Subject: [PATCH 1/3] create a "index" options for the "serve" method --- aqua.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/aqua.ts b/aqua.ts index e407127..89b46eb 100644 --- a/aqua.ts +++ b/aqua.ts @@ -139,6 +139,7 @@ type RoutingSchema = { export interface RoutingOptions { schema?: RoutingSchema; + index?: string; } export enum MiddlewareType { @@ -401,9 +402,9 @@ export default class Aqua { private async handleStaticRequest( req: Request, - { path, folder }: { path: string; folder: string }, + { path, folder, index }: { path: string; folder: string, index?: string; }, ): Promise { - const requestedPath = parseRequestPath(req.url); + let requestedPath = parseRequestPath(req.url); const resourcePath: string = requestedPath.replace(path, ""); const extension: string = resourcePath.replace( /.*(?=\.[a-zA-Z0-9_]*$)/, @@ -414,9 +415,14 @@ export default class Aqua { : null; try { + + if(index !== "" && index !== undefined && index !== null){ + requestedPath = index; + } + return { headers: contentType ? { "Content-Type": contentType } : undefined, - content: await Deno.readFile(`${folder}/${resourcePath}`), + content: await Deno.readFile(`${folder}/${requestedPath}`), }; } catch { return await this.getFallbackHandlerResponse( @@ -602,8 +608,11 @@ export default class Aqua { this.staticRoutes.push({ folder: folder.replace(/\/$/, "") + "/", path: path.replace(/\/$/, "") + "/", - responseHandler: async (req) => - await this.handleStaticRequest(req, { path, folder }), + responseHandler: async (req) => { + + + return await this.handleStaticRequest(req, { path, folder, index: options.index }); + }, options, }); return this; From 1c5a3a464be491444a2c9b07e2894fa42e4b45b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Feli=CE=9E?= Date: Tue, 5 Jul 2022 08:51:12 +0000 Subject: [PATCH 2/3] update docs for the "index" route option --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 41fe991..ed97f0d 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,18 @@ app.serve("mystaticfolder", "/public"); // A GET request to /public/test.txt would serve the local file at mystaticfolder/test.txt ``` +(Optional) Static route with default index page + +You could define a default index page for your static routes such as `index.html` + +```typescript +app.serve("mystaticfolder", "/", { + index: "index.html", +}); +``` + +so when a user requests `/`, the server will serve the file `mystaticfolder/index.html` + ### Regex paths You can provide a RegExp object instead of a string and receive the matches. From 8a4abe6be59def6d5a2c9c87675cf7b5a5a93381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Feli=CE=9E?= Date: Tue, 5 Jul 2022 08:59:37 +0000 Subject: [PATCH 3/3] fix "requestedPath" to "resourcePath" --- aqua.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aqua.ts b/aqua.ts index 89b46eb..2afdb20 100644 --- a/aqua.ts +++ b/aqua.ts @@ -422,7 +422,7 @@ export default class Aqua { return { headers: contentType ? { "Content-Type": contentType } : undefined, - content: await Deno.readFile(`${folder}/${requestedPath}`), + content: await Deno.readFile(`${folder}/${resourcePath}`), }; } catch { return await this.getFallbackHandlerResponse(