RejectedSoftware Forums

Sign up

Serving static files with nested routers.

Hello everyone.

I'm familiarizing myself with vibe.d, but I encountered - I assume - a small problem regarding serving static files while having nested URL routers.

Here's a theoretical situation:

URLRouter getAssembledURLRouter()
{	
    URLRouter router = new URLRouter();
    URLRouter sub_router = getSubRouter();

    router.get("/", &indexResource);
    router.any("*", sub_router); // Append the nested router.
    router.get("*", serveStaticFiles("static/"));
    
    return router;
}

URLRouter getSubRouter()
{
    URLRouter sub_router = new URLRouter("/nested");
    sub_router.get("/", &someResource);
    sub_router.get("/blah", &someBlahResource);

    return sub_router;
}

The problem

Let's assume we have a style.css file. While accessing / the file is served a-ok. But when I access /nested or /nested/blah the file is tried to be served with path /nested/style.css which of course results in 404.

Question

How do I force vibe.d to always serve static files on path / while having nested routers structure as shown above?

Cheers! :)

Re: Serving static files with nested routers.

On Thu, 16 Jul 2015 18:13:21 GMT, Wiktor Żurawik wrote:

Hello everyone.

I'm familiarizing myself with vibe.d, but I encountered - I assume - a small problem regarding serving static files while having nested URL routers.

Here's a theoretical situation:

URLRouter getAssembledURLRouter()
{	
    URLRouter router = new URLRouter();
    URLRouter sub_router = getSubRouter();

    router.get("/", &indexResource);
    router.any("*", sub_router); // Append the nested router.
    router.get("*", serveStaticFiles("static/"));
    
    return router;
}

URLRouter getSubRouter()
{
    URLRouter sub_router = new URLRouter("/nested");
    sub_router.get("/", &someResource);
    sub_router.get("/blah", &someBlahResource);

    return sub_router;
}

The problem

Let's assume we have a style.css file. While accessing / the file is served a-ok. But when I access /nested or /nested/blah the file is tried to be served with path /nested/style.css which of course results in 404.

Question

How do I force vibe.d to always serve static files on path / while having nested routers structure as shown above?

Cheers! :)

One thing you could do is to add a second serveStaticFiles route and pass "/nested/" as the HTTPFileServerSettings.serverPathPrefix.

Another, more efficient, possibility would be to add a "*" route right before the serveStaticFiles one and remove the "/nested" prefix from HTTPServerRequest.path therein, if there is one. If you'd want to generalize this, you could of course also drop any prefix up to the last '/'.

Re: Serving static files with nested routers.

On Thu, 16 Jul 2015 19:17:14 GMT, Sönke Ludwig wrote:

One thing you could do is to add a second serveStaticFiles route and pass "/nested/" as the HTTPFileServerSettings.serverPathPrefix.

Another, more efficient, possibility would be to add a "*" route right before the serveStaticFiles one and remove the "/nested" prefix from HTTPServerRequest.path therein, if there is one. If you'd want to generalize this, you could of course also drop any prefix up to the last '/'.

The first method runs just fine. That's what I was looking for. Thanks for the solution. ;)