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

deno serve: support using the fetch handler as the default export #23844

Open
pomdtr opened this issue May 16, 2024 · 2 comments
Open

deno serve: support using the fetch handler as the default export #23844

pomdtr opened this issue May 16, 2024 · 2 comments
Labels
serve suggestion suggestions for new features (yet to be agreed)

Comments

@pomdtr
Copy link

pomdtr commented May 16, 2024

In https://val.town, you declare a server by using the fetch handler as your module default export.

export default function(req: Request) {
   return new Response("hello world")
}

One interesting property is that it makes it easy to add middlewares on top of servers:

// cors.ts
export function cors(handler) {
    return async (req) {
       const resp = await handler(req)
       resp.headers.set("access-control-allow-origin", "*");
       return resp
    }
}

// cors_server.ts
import handler from "./server.ts"

export default cors(handler)

What do you think of adding support for this convention in deno serve ?

@pomdtr pomdtr changed the title deno serve: support using the handler as the default export deno serve: support using the fetch handler as the default export May 16, 2024
@bartlomieju
Copy link
Member

Can you explain a bit more what is the proposed convention here? Using any default export as a handler?

@pomdtr
Copy link
Author

pomdtr commented May 16, 2024

Currently deno serve expect the default export to be an object, with a fetch method:

export default {
  async fetch(request) {
    return new Response("Hello world!");
  },
};

I think exporting the fetch handler directly should also be supported.

export default async function(request) {
  return new Response("Hello world!");
}

@bartlomieju bartlomieju added suggestion suggestions for new features (yet to be agreed) serve labels May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
serve suggestion suggestions for new features (yet to be agreed)
Projects
None yet
Development

No branches or pull requests

2 participants