Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 13 additions & 4 deletions aqua.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type RoutingSchema = {

export interface RoutingOptions {
schema?: RoutingSchema;
index?: string;
}

export enum MiddlewareType {
Expand Down Expand Up @@ -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<Response> {
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_]*$)/,
Expand All @@ -414,6 +415,11 @@ 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}`),
Expand Down Expand Up @@ -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;
Expand Down