Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
orgsofthq opened this issue Oct 11, 2021 · 2 comments
Open
Labels
enhancement New feature or request

Comments

@orgsofthq
Copy link

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/oak@v9.0.1/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/oak@v9.0.1/httpError.ts:109:12)
     at send (https://deno.land/x/oak@v9.0.1/send.ts:127:15)
     at async file:///src/src/entry.ts:4:5
     at async dispatch (https://deno.land/x/oak@v9.0.1/middleware.ts:16:13)
     at async EventTarget.#handleRequest (https://deno.land/x/oak@v9.0.1/application.ts:198:17)
5 | response: { status: 404, type: undefined, hasBody: false, writable: true }
6 | request: { url: "[redacted]", method: "GET", hasBody: true }
@jcs224
Copy link
Contributor

jcs224 commented Nov 21, 2021

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.

@kitsonk kitsonk added the enhancement New feature or request label Dec 19, 2021
@kitsonk
Copy link
Collaborator

kitsonk commented Dec 19, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants