RejectedSoftware Forums

Sign up

Pages: 1 2

How server static page?

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.

Re: How server static page?

I know that I can do:

router.get("/", serveStaticFile("public/index.html"));

but I want to understand how to use "&" (ampersand) with static pages. Like router.get("/", &index);

Re: How server static page?

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;

Re: How server static page?

I mean that in my example part:

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

do not work. It's redirect to index.html URL, but not open file (404 error).

What I should to add in this block of code to get it's open index page, that located in public folder.

Re: How server static page?

On Wed, 10 Jun 2015 08:15:46 GMT, Suliman wrote:

I mean that in my example part:

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

do not work. It's redirect to index.html URL, but not open file (404 error).

What I should to add in this block of code to get it's open index page, that located in public folder.

It does work, it just doesn't do what you expected. You're asking it to redirect "/" to "/index.html" and it does that. But since you didn't provide any handler for "/index.html", it returns a 404.

Why don't you want to use serveStaticFile if youwant to do exactly what it does ?
You can look at the code here: https://github.com/rejectedsoftware/vibe.d/blob/d3fb879f1bd58e63f49c4693501974467f45f04a/source/vibe/http/fileserver.d#L114

Probably what you want is:

router.get("/", staticRedirect("/index.html")),
      .get("*", serveStaticFiles("public/"));

That will redirect "/" to "/index.html" (just like your handler), and serve content of files that are matching URL in "public/".

Note that it's serveStaticFiles (plural). That will allow you to serve the static content you want.
serveStaticFiles source: https://github.com/rejectedsoftware/vibe.d/blob/c9d47138b14570332920aeaf73621e038c6d5e93/source/vibe/http/fileserver.d#L28

Re: How server static page?

Why don't you want to use serveStaticFile if youwant to do exactly what it does ?

Because I do not understand why I can't do serveStaticFiles in handler ("index" in my case) and must do serveStaticFiles inside get.

I am trying to understand it, because if I would not understand logic of this case I would have problems with more complicated code.

Re: How server static page?

Am 10.06.2015 um 11:52 schrieb Suliman:

Why don't you want to use serveStaticFile if youwant to do exactly what it does ?

Because I do not understand why I can't do serveStaticFiles in handler ("index" in my case) and must do serveStaticFiles inside get.

I am trying to understand it, because if I would not understand logic of this case I would have problems with more complicated code.

You could also do this:

static this() {
   // ...
   router.get("/", &index);
   router.get("/index.html", &serveIndex);
}

void serveIndex(HTTPServerRequest req, HTTPServerResponse res)
{
   static HTTPServerRequestDelegateS file_server;
   if (!file_server) file_server = serveStaticFile("/public/index.html");
   file_server(req, res);
}

There is a private sendFile method in vibe.http.fileserver that we
could also make public to better support this kind of use case. Do you
have a particular reason why directly using serveStaticFile(s) with
router.get() is not ideal, or do you just want to understand the logic
behind it?

Re: How server static page?

just want to understand the logic behind it?

Just it.

Re: How server static page?

that we could also make public to better support this kind of use case

I think that it would be more logical. Because it will separate handling and processing requests parts.

Re: How server static page?

On Wed, 10 Jun 2015 10:52:59 GMT, Suliman wrote:

that we could also make public to better support this kind of use case
I think that it would be more logical. Because it will separate handling and processing requests parts.

The current API was chosen because it is more convenient (less to type), but I also think that there is no reason not to make the direct API accessible as well.

I've added a ticket: #1130

Pages: 1 2