RejectedSoftware Forums

Sign up

serveStaticFiles not working as expected

So I have the basic hello world code running from the first example on the Documentation page.
I wanted to add the ability to serve static files, so I added a line that I thought would do what I wanted.

auto router = new URLRouter;
router.get("/index.html", &index);
router.get("/static/*", serveStaticFiles("public/"));

However...

$ file public/other.html
  public/other.html: HTML document, ASCII text
$ curl localhost:8729/static/other.html       
  404 - Not Found

What am I missing?

Re: serveStaticFiles not working as expected

What am I missing?

This :
auto fsettings = new HTTPFileServerSettings;
fsettings.serverPathPrefix = "/static";
...
auto router = new URLRouter;
router.get("/index.html", &index);
router.get("/static/*", serveStaticFiles("public/"));

Re: serveStaticFiles not working as expected

On Fri, 24 Jan 2014 07:31:38 GMT, Ty Overby wrote:

What am I missing?

This :

auto fsettings = new HTTPFileServerSettings;
fsettings.serverPathPrefix = "/static";
 
auto router = new URLRouter;
router.get("/index.html", &index);
router.get("/static/*", serveStaticFiles("public/",fsettings)); // forgot this in my first answer

Re: serveStaticFiles not working as expected

On Fri, 24 Jan 2014 08:39:37 GMT, Uplink_Coder wrote:

On Fri, 24 Jan 2014 07:31:38 GMT, Ty Overby wrote:

What am I missing?
This :

auto fsettings = new HTTPFileServerSettings;
fsettings.serverPathPrefix = "/static";
 
auto router = new URLRouter;
router.get("/index.html", &index);
router.get("/static/*", serveStaticFiles("public/",fsettings)); // forgot this in my first answer

Thank you, that did the trick! Is there any reason that the documentation at http://vibed.org/docs under the Routing section is out of date? Am I using a newer version of Vibe.d?

Re: serveStaticFiles not working as expected

Also, why is the form that I was using valid at all? Was it actually doing something that I wasn't detecting?

Re: serveStaticFiles not working as expected

I'll prepare a pull requeset :D
HTTPFileServerSettings should have been there at least since 0.7.17
maybe longer ...

Re: serveStaticFiles not working as expected

Hmm browsers can be very forgiving ...
to say more I would need to see your code

Re: serveStaticFiles not working as expected

Am 25.01.2014 08:41, schrieb Ty Overby:

Also, why is the form that I was using valid at all? Was it actually doing something that I wasn't detecting?

The problem is that the serveStaticFiles function, when it gets a
routed request like "/static/image.png", has no way to know that the
matching route looks like "/static/*" and it should drop the prefix. So
in this case it expects the file to be at "./static/static/image.png".

The example in the documentation assumes that all the static files are
placed directly in "/", so "/image.png" would be looked up in
"./public/image.png" - and for this case it should still be valid.

But there should definitely be a short example of using the advanced
settings version of serveStaticFiles, if not in the main
documentation, at least on the API page for serveStaticFiles.

Re: serveStaticFiles not working as expected

On Mon, 27 Jan 2014 20:03:03 +0100, Sönke Ludwig wrote:

Am 25.01.2014 08:41, schrieb Ty Overby:

Also, why is the form that I was using valid at all? Was it actually doing something that I wasn't detecting?

The problem is that the serveStaticFiles function, when it gets a
routed request like "/static/image.png", has no way to know that the
matching route looks like "/static/*" and it should drop the prefix. So
in this case it expects the file to be at "./static/static/image.png".

Did you meant "./public/static/image.png" ?

The example in the documentation assumes that all the static files are
placed directly in "/", so "/image.png" would be looked up in
"./public/image.png" - and for this case it should still be valid.

But there should definitely be a short example of using the advanced
settings version of serveStaticFiles, if not in the main
documentation, at least on the API page for serveStaticFiles.

Despite of being an old thread, I have run in the same problem, what about giving the following example:

router.get("/styles/*", serveStaticFiles("public/"));

while addressing the .css directly in diet

link(rel= 'stylesheet', type='text/css', href='/style/style.css`

Or additionaly about extending the allowed expressions with (*) Example:

router.get("/static/(*)", serveStaticFiles("public/"));`

So that only the matching part (*) of the path is passed to serveStaticFiles?

The use of the other "version" with:
auto fsettings = new HTTPFileServerSettings;<br>fsettings.serverPathPrefix = "/static";

is not nice at all.

  1. point: It seams, that no checking of the existence of path on the file system is done (as string) is done.

Best regards mt.

Re: serveStaticFiles not working as expected

On Wed, 06 Jan 2016 12:09:37 GMT, Martin Tschierschke wrote:

On Mon, 27 Jan 2014 20:03:03 +0100, Sönke Ludwig wrote:

Am 25.01.2014 08:41, schrieb Ty Overby:

Also, why is the form that I was using valid at all? Was it actually doing something that I wasn't detecting?

The problem is that the serveStaticFiles function, when it gets a
routed request like "/static/image.png", has no way to know that the
matching route looks like "/static/*" and it should drop the prefix. So
in this case it expects the file to be at "./static/static/image.png".

Did you meant "./public/static/image.png" ?

Yes, sorry for the confusion.

The example in the documentation assumes that all the static files are
placed directly in "/", so "/image.png" would be looked up in
"./public/image.png" - and for this case it should still be valid.

But there should definitely be a short example of using the advanced
settings version of serveStaticFiles, if not in the main
documentation, at least on the API page for serveStaticFiles.

Despite of being an old thread, I have run in the same problem, what about giving the following example:

router.get("/styles/*", serveStaticFiles("public/"));

while addressing the .css directly in diet

link(rel= 'stylesheet', type='text/css', href='/style/style.css`

Or additionaly about extending the allowed expressions with (*) Example:

router.get("/static/(*)", serveStaticFiles("public/"));`

So that only the matching part (*) of the path is passed to serveStaticFiles?

The use of the other "version" with:
auto fsettings = new HTTPFileServerSettings;<br>fsettings.serverPathPrefix = "/static";

is not nice at all.

The problem is that the router and the file server are completely distinct and there is no way for the file server to get this information.

However, an idea would be to add a separate function that is used like this: router.serveStaticFiles("/styles/", "public/styles/");

void serveStaticFiles(URLRouter router, string request_folder, string local_folder)
{
  auto settings = new HTTPFileServerSettings;
  settings.serverPathPrefix = request_folder;
  router.get(request_folder~"*", serveStaticFiles(local_folder, settings));
}

The only issue is to find a good place for it. It would be a bit of a pity to couple the two modules together, but if nothing else helps, letting vibe.http.fileserver import vibe.http.router might be acceptable. ... or it could be added as a feature of the high-level web framework (vibe.web.web).

  1. point: It seams, that no checking of the existence of path on the file system is done (as string) is done.

You mean for the directory passed to serveStaticFiles, right? That's true and I think that would be a good idea. I'll add that.