oak icon indicating copy to clipboard operation
oak copied to clipboard

Unable to serve static files with deploy: Deno.stat is not a function

Open orgsofthq opened this issue 4 years ago • 2 comments
trafficstars

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 }

orgsofthq avatar Oct 11 '21 16:10 orgsofthq

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.

jcs224 avatar Nov 21 '21 11:11 jcs224

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.

kitsonk avatar Dec 19 '21 22:12 kitsonk