-
Notifications
You must be signed in to change notification settings - Fork 0
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
Middlewares Pattern #4
Comments
@Abiti-936 One idea is to compose a import cors from "https://deno.land/x/edge_cors/src/cors.ts";
import { createRouter } from "https://deno.land/x/http_router/mod.ts";
const api = createRouter({
"/hello": () => new Response("hello"),
});
const router = createRouter({
"/api": cors(api),
});
function log(handler: (req: Request) => Promise<Response> | Response) {
return async (req: Request) => {
const res = await handler(req);
console.log({
req, res,
});
return res;
};
}
await Deno.serve(log(router)); If the router is self-recursive, it is divisible and most expressive and beautiful. However, it requires some ingenuity in implementation because of the need to perform URL pattern matching. The middleware pattern is difficult to formulate a standard API. In fact, middleware was discussed in Deno, but is not currently implemented for the same reason. denoland/std#1295 Also, Any thoughts on these? |
I mean, I don't expect you to add the use method, but you can add middlewares pattern like Also the Upper Implementation is Just Really Good. |
I have tried the Upper log example by changing it cors & it's working. import { createRouter } from "https://deno.land/x/http_router/mod.ts";
import edgeCors from "https://deno.land/x/edge_cors@0.2.1/src/cors.ts";
import { options } from "./corsOptions.ts";
const router = createRouter({
"/hello": () => new Response("hello")
});
export function cors(handler: (request: Request) => Promise<Response> | Response) {
return async (request: Request) => {
const response = await handler(request);
return edgeCors(request, response, options);
};
}
await Deno.serve(cors(router)); Thanks. |
@Abiti-936 A number of important features have been added, including nested route and base path. https://github.com/httpland/http-router/releases/tag/1.2.0 |
That's Great. I will take a look at the new release. Thanks. |
I liked these Routing Library & I am now using it with My Supabase Edge Functions, & it's just a great library.
One, thing that is cool to have is middlewares pattern, to Add CORS Header, Check for Authorization Headers & more... before & after Handling Routes.
For Example, The Pattern can be like these :-
I think these can make a whole lot of use cases possible. Thanks.
The text was updated successfully, but these errors were encountered: