oak
oak copied to clipboard
Unable to serve static files with deploy: Deno.stat is not a function
oak is advertised as compatible with deno deploy, but doesn't seem to be able to serve static files.
With the minimal repro below, everything works fine with deployctl locally or deno run, but once deployed it errors upon testing.
Would appreciate any suggestions to workaround or fix.
repro
mod.ts
import { Application, send } from "https://deno.land/x/[email protected]/mod.ts";
const app = new Application();
app.use(async (context) => {
await send(context, context.request.url.pathname, {
root: `${Deno.cwd()}/static`,
index: "index.html",
});
});
await app.listen({ port: 80});
http response
Internal Server Error
deploy logs
[uncaught application error]: InternalServerErrorError - Deno.stat is not a function
--
4 | at createHttpError (https://deno.land/x/[email protected]/httpError.ts:109:12)
at send (https://deno.land/x/[email protected]/send.ts:127:15)
at async file:///src/src/entry.ts:4:5
at async dispatch (https://deno.land/x/[email protected]/middleware.ts:16:13)
at async EventTarget.#handleRequest (https://deno.land/x/[email protected]/application.ts:198:17)
5 | response: { status: 404, type: undefined, hasBody: false, writable: true }
6 | request: { url: "[redacted]", method: "GET", hasBody: true }
Yeah, the send method seems to not work with Deploy. My workaround is something like this:
// Package to help automatically infer the correct mime type based on extension
import mime from 'https://cdn.skypack.dev/mime-types'
// use this inside your middleware instead of 'send'
const requestMimeType = mime.lookup(ctx.request.url.pathname)
const responseMimeType = mime.contentType(requestMimeType)
ctx.response.headers.set('Content-Type', responseMimeType)
ctx.response.body = await Deno.readTextFile(Deno.cwd() + ctx.request.url.pathname)
Fixed it in Deploy for me. But, this should probably be addressed in Oak too, or at least noted in documentation.
Deno.stat() is being added to Deploy (though it might not totally give everything needed). "Static" files in Deploy is a bit of a moving target at the moment. I will try to make .send() more compatible with it.