cxx
cxx•11mo ago

Oak: Remove HTML extensions when serving files

Using Oak, how can I serve home.html as /home (or /home/). I'm aware that Context#send exists but I'm not sure if it'll work with my use case. Thanks in advance!
2 Replies
Deno AI Helper
Deno AI Helper•11mo ago
The documentation does not provide a specific example of serving HTML files without the .html extension using Oak. However, you can achieve this by setting up your Oak routes to match the desired paths (like /home or /home/) and then use the ctx.send() method to serve the corresponding HTML file. Here's a general example:
import { Application, Router } from "https://deno.land/x/oak/mod.ts";

const router = new Router();

router.get("/home", async (ctx) => {
await ctx.send({
root: `${Deno.cwd()}/path/to/your/html/files`,
path: "home.html"
});
});

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 8000 });
import { Application, Router } from "https://deno.land/x/oak/mod.ts";

const router = new Router();

router.get("/home", async (ctx) => {
await ctx.send({
root: `${Deno.cwd()}/path/to/your/html/files`,
path: "home.html"
});
});

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 8000 });
In this example, a GET request to /home will serve the home.html file. Please replace "${Deno.cwd()}/path/to/your/html/files" with the actual path to your HTML files. Remember, this is a general advice and not explicitly stated in the documentation. Always test your code thoroughly to ensure it works as expected. 🦕
cxx
cxx•11mo ago
I have a static file generator so I can't "predefined" routes