Search code examples
urlpathcorshttp-status-code-404freshjs

Why does navigating to a path with double slash result 404?


In routes/api/[...url].ts I have this file:

import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
  GET(_req, ctx) {
    return new Response(ctx.url.href);
  },
};

If I navigate to localhost:8000/api/A/B, it works fine. In my understanding, the filename [...url].ts should be correct, and a URL with double slash is valid, so I expect localhost:8000/api/A//B should work as well. But it results with 404. Why is that the case?

My goal is to build a CORS proxy, so there should be a double slash in the URL, e.g. http://localhost:8001/api/https://example.com/


Solution

  • Upgrading Fresh to 1.6.8 solves the status code problem, but the URL is still processed. Instead of using ctx.url.href, use req.url to get the raw URL. ctx is a object from Fresh with interface FreshContext, while req is the native object with interface Request.