webzone/server.ts

31 lines
816 B
TypeScript

import { Application, Router } from "https://deno.land/x/oak@v11.1.0/mod.ts";
const app = new Application();
app.addEventListener("error", (evt) => {
// Will log the thrown error to the console.
console.dir(evt.error);
});
// First we try to serve static files from the dist folder. If that fails, we
// fall through to the router below.
app.use(async (ctx, next) => {
await ctx.send({
root: `${Deno.cwd()}/dist`,
index: "index.html",
hidden: true,
}).catch(async () => {
ctx.response.status = 404;
ctx.response.body = await Deno.readTextFile(`${Deno.cwd()}/dist/404.html`);
await next();
});
});
const router = new Router();
// After creating the router, we can add it to the app.
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 8000 });