On Mon, 08 Jun 2015 13:19:48 GMT, Suliman wrote:

I am still trying to understand how to server static page. I looked at examples, but I do not understand why modified code co not work:

import std.stdio;

import vibe.core.core;
import vibe.core.log;
import vibe.http.router;
import vibe.http.server;
import vibe.http.fileserver;
import vibe.web.rest;

void main()
{
	auto router = new URLRouter;
	router.get("/", &index);

	auto settings = new HTTPServerSettings;
	settings.port = 8080;
	listenHTTP(settings, router);

	runEventLoop();
}

void index(HTTPServerRequest req, HTTPServerResponse res)
{
	res.redirect("/index.html");
}

It's clear, that code do not have serveStaticFiles part, so it would now work.

Example include line:
router.get("*", serveStaticFiles("./public/",));
but I can't understand why after handling index.html I should handle all ("*") requests?

Please explain me why I should write in such way, I do not see any logic here.

I'm not sure of what you are trying to do here. My understanding is that the missing piece is just the chaining of router. E.g you can do the following:

router.get("/", &index)
      .get("/index.html", &serveMain)
      .get("/data/*", &serveData);

Note that:

  • Globbing ('*') can only be at the end;
  • This will handle only 'GET' request, you have post, delete_ for the other (or any);
  • You cannot shadow a route: with router.get("*", &serveAll).get("/", &serveIndex);, the first handler will always get called;