├── public
│ ├── favicon.ico
│ └── style.css
├── view
│ └── index.html
└── index.ts
based on Mark Tyers's answer ❤️
Of course, there is a better way to serve static files with nginx!
├── public
│ ├── favicon.ico
│ └── style.css
├── view
│ └── index.html
└── index.ts
based on Mark Tyers's answer ❤️
Of course, there is a better way to serve static files with nginx!
| import { Application, Context, Router, send, Status } from "https://deno.land/x/oak/mod.ts" | |
| // check file exist | |
| async function checkFileExist(ctx: Context) { | |
| const path = `${Deno.cwd()}/${ctx.request.url.pathname}`; | |
| try { | |
| const fileInfo = await Deno.lstat(path); | |
| return fileInfo.isFile; | |
| } catch { | |
| console.log(path); | |
| return false; | |
| } | |
| } | |
| const router = new Router(); | |
| const app = new Application(); | |
| // serve home page | |
| router.get("/", async (ctx: Context) => { | |
| await send(ctx, "/", { | |
| root: `${Deno.cwd()}/view`, | |
| index: "index.html", | |
| }); | |
| }); | |
| // serve favoicon.ico | |
| router.get("/favicon.ico", async (ctx: Context) => { | |
| await send(ctx, "/favicon.ico", { | |
| root: `${Deno.cwd()}/public`, | |
| index: "favicon.ico", | |
| }); | |
| }); | |
| app.use(router.routes()); | |
| app.use(router.allowedMethods()); | |
| // serve static file | |
| app.use(async (ctx, next) => { | |
| if (await checkFileExist(ctx)) { | |
| await send(ctx, ctx.request.url.pathname, { | |
| root: `${Deno.cwd()}`, | |
| }); | |
| } else { | |
| next(); | |
| } | |
| }); | |
| // serving on http://localhost:8000 | |
| await app.listen({ port: 8000 }); |