May 07, 2015 Re: vibed: how to use pure HTML instead of template engine? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Thursday, 7 May 2015 at 08:25:30 UTC, Suliman wrote: >> You're not setting a port. >> >> add: >> settings.port = 8080; >> >> before listenHTTP(); >> >> then it'll work. > > It's do not help :( You're sure? My app.d is: import std.stdio; import vibe.d; shared static this(){ auto router = new URLRouter; router.get("*", serveStaticFiles("./public/")); auto settings = new HTTPServerSettings; settings.port = 8080; listenHTTP(settings, router); } And i have a file public/index.html <html> <body> <h1> Hi </h1> </body> </html> When I navigate to localhost:8080 i can see a big "Hi". |
May 07, 2015 Re: vibed: how to use pure HTML instead of template engine? | ||||
---|---|---|---|---|
| ||||
Posted in reply to wobbles | On Thursday, 7 May 2015 at 09:08:53 UTC, wobbles wrote: > On Thursday, 7 May 2015 at 08:25:30 UTC, Suliman wrote: >>> You're not setting a port. >>> >>> add: >>> settings.port = 8080; >>> >>> before listenHTTP(); >>> >>> then it'll work. >> >> It's do not help :( > > You're sure? > > My app.d is: > import std.stdio; > import vibe.d; > > shared static this(){ > auto router = new URLRouter; > > router.get("*", serveStaticFiles("./public/")); > > auto settings = new HTTPServerSettings; > settings.port = 8080; > > listenHTTP(settings, router); > } > > And i have a file > public/index.html > <html> > <body> > <h1> Hi </h1> > </body> > </html> > > When I navigate to localhost:8080 i can see a big "Hi". I see you have you're function called setupServer() Are you using the vibe default main or you're own main function? It's possible you're not setting up the event loop correctly. At -> http://vibed.org/docs search for "The main function" and you'll see how to set it up correctly. |
May 07, 2015 Re: vibed: how to use pure HTML instead of template engine? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Thursday, 7 May 2015 at 08:25:30 UTC, Suliman wrote: >> You're not setting a port. >> >> add: >> settings.port = 8080; >> >> before listenHTTP(); >> >> then it'll work. > > It's do not help :( This should work, put it in your `app.d` file: import vibe.d; shared static this() { auto settings = new HTTPServerSettings; settings.port = 8080; settings.bindAddresses = ["::1", "127.0.0.1"]; auto router = new URLRouter; router.get("*", serveStaticFiles("./public/")); listenHTTP(settings, router); logInfo("Please open http://127.0.0.1:8080/ in your browser."); } Mind you, don't forget the `.` before the forward slash in serveStaticFiles("./public/"). Any file in the folder `public` should now be "navigate-able to" via: http://127.0.0.1:8080/index.html or http://localhost/index.html For starters, don't make your own custom main method. Just create a vibe.d project and paste the above code into app.d. Later you can have a more sophisticated handling of requests. |
May 07, 2015 Re: vibed: how to use pure HTML instead of template engine? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | On Thursday, 7 May 2015 at 09:27:39 UTC, Chris wrote: > On Thursday, 7 May 2015 at 08:25:30 UTC, Suliman wrote: >>> You're not setting a port. >>> >>> add: >>> settings.port = 8080; >>> >>> before listenHTTP(); >>> >>> then it'll work. >> >> It's do not help :( > > This should work, put it in your `app.d` file: > > import vibe.d; > > shared static this() > { > > auto settings = new HTTPServerSettings; > settings.port = 8080; > settings.bindAddresses = ["::1", "127.0.0.1"]; > > auto router = new URLRouter; > router.get("*", serveStaticFiles("./public/")); > listenHTTP(settings, router); > > logInfo("Please open http://127.0.0.1:8080/ in your browser."); > } > > Mind you, don't forget the `.` before the forward slash in serveStaticFiles("./public/"). > > Any file in the folder `public` should now be "navigate-able to" via: > > http://127.0.0.1:8080/index.html > or > http://localhost/index.html > > For starters, don't make your own custom main method. Just create a vibe.d project and paste the above code into app.d. > > Later you can have a more sophisticated handling of requests. Do this 1. dub init suliman vibe.d 2. You might see an info message like this Deprecated use of init type. Use --type=[vibe.d | deimos | minimal] in future. Successfully created an empty project in '/home/christoph/D/vibed/Tests/suliman'. 3. Now you have a small project. Go to the folder `suliman/source` and open the file app.d 4. Replace the whole code in there with import vibe.d; shared static this() { auto settings = new HTTPServerSettings; settings.port = 8080; settings.bindAddresses = ["::1", "127.0.0.1"]; auto router = new URLRouter; router.get("*", serveStaticFiles("./public/")); listenHTTP(settings, router); logInfo("Please open http://127.0.0.1:8080/ in your browser."); } 5. Save an build the project 6. Create a file called `index.html` in the (sub)folder `public` that is in the folder `suliman`, i.e. `suliman/public/` 7. Paste this code into it: <html> <p> Hello, Suliman! </p> </html> 8. Save it and navigate in your browser to http://127.0.0.1:8080/index.html You should see "Hello, Suliman!" in your browser window now. Hope this helped you. C. |
May 07, 2015 Re: vibed: how to use pure HTML instead of template engine? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | Later you can have more sophisticated methods, e.g. if you want to handle query strings you could do something like this: import vibe.d; shared static this() { auto settings = new HTTPServerSettings; settings.port = 8080; settings.bindAddresses = ["::1", "127.0.0.1"]; auto router = new URLRouter; router.get("*", serveStaticFiles("./public/")); /* This is the new bit */ router.any("*", &handleRequest); listenHTTP(settings, router); logInfo("Please open http://127.0.0.1:8080/ in your browser."); } void handleRequest(HTTPServerRequest req, HTTPServerResponse res) { if (!req.query.length) return; auto request = req.query; // Do something fancy with the request // ... // Create a result string result; // ... // Return the result to the client res.writeBody(cast(ubyte[])result); // The client will receive this. } |
May 07, 2015 Re: vibed: how to use pure HTML instead of template engine? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | shared static this() { auto router = new URLRouter; router.get("/", &root); auto settings = new HTTPServerSettings; settings.port = 8080; listenHTTP(settings, router); } void root(HTTPServerRequest req, HTTPServerResponse res) { serveStaticFiles("public/"); } 1. Do I need write "./public/" ? In examples often simply "public/" 2. What incoming parameters ("HTTPServerRequest req, HTTPServerResponse res") mean? Why I should to specify them? 3. Why code with: "res.writeBody("Hello, World!", "text/plain");" and "router.get("*", serveStaticFiles("./public/"));" also work, but my variant (see code above) do not load say that page not found? 4. How to specify page that I need to load, why in examples there is only link to folder like public? But what if I want to load public/foo.html? |
May 07, 2015 Re: vibed: how to use pure HTML instead of template engine? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Thursday, 7 May 2015 at 18:59:13 UTC, Suliman wrote: > 1. Do I need write "./public/" ? In examples often simply "public/" will work too. even "public" it goes trough Path struct, see: https://github.com/rejectedsoftware/vibe.d/blob/11578aa956a9b3b0e305d655f9668a867fdd89bd/source/vibe/inet/path.d > 2. What incoming parameters ("HTTPServerRequest req, HTTPServerResponse res") mean? Why I should to specify them? HTTPServerRequest contains all data that the client sends, e.g. headers, cookies, source ip etc. see: https://github.com/rejectedsoftware/vibe.d/blob/11578aa956a9b3b0e305d655f9668a867fdd89bd/source/vibe/http/server.d#L584 HTTPServerResponse is the response you send back. https://github.com/rejectedsoftware/vibe.d/blob/11578aa956a9b3b0e305d655f9668a867fdd89bd/source/vibe/http/server.d#L788 you woud want to set at least its body, as you do below > 3. Why code with: "res.writeBody("Hello, World!", "text/plain");" > and "router.get("*", serveStaticFiles("./public/"));" also work, but my variant (see code above) do not load say that page not found? what exactely does not work? please link code (ideally https://gist.github.com/ ) > 4. How to specify page that I need to load, why in examples there is only link to folder like public? But what if I want to load public/foo.html? public servers public files, its more or less a static webserver which checks if a file exists and then serves that. if you want to dynamically modify content you send you need to use the templating or do your own string magic. generally it seems you do not fully understand the concept of how these web frameworks work. i think you should either read vibe.d's source code or more read how other such frameworks work e.g. http://www.sinatrarb.com/documentation.html has pretty good documentations and books that explain the inner workings (but mind, ruby is a dynamically typed language). then i also can recommend that you check out vibe.d's github repositories and read trough all the example projects that come with it. e.g. https://github.com/rejectedsoftware/vibe.d/tree/11578aa956a9b3b0e305d655f9668a867fdd89bd/examples/app_skeleton |
May 07, 2015 Re: vibed: how to use pure HTML instead of template engine? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Thursday, 7 May 2015 at 18:59:13 UTC, Suliman wrote: > shared static this() > { > auto router = new URLRouter; > router.get("/", &root); > > auto settings = new HTTPServerSettings; > settings.port = 8080; > listenHTTP(settings, router); > } > > > void root(HTTPServerRequest req, HTTPServerResponse res) > { > serveStaticFiles("public/"); > } i missed this in the answer sorry, its clear that its not working you are saying that ONLY requests to "/" shall be served with the staticFiles. this makes no sense. for static files you would want a catch all route at the end, and before that you define a few other routes that serve specific strings. check this: https://github.com/rejectedsoftware/vibe.d/blob/11578aa956a9b3b0e305d655f9668a867fdd89bd/examples/app_skeleton/source/app.d#L18 router.get("/", &showHome); // GET / is handled by the showHome function router.get("/about", staticTemplate!"about.dt"); // GET /about is handlet by the template router.get("*", serveStaticFiles("public")); // every other get request goes here and checks if a filename exists within the public folder. so e.g. GET /foo.html is either served public/foo.html if it exists or returns an 404 |
May 08, 2015 Re: vibed: how to use pure HTML instead of template engine? | ||||
---|---|---|---|---|
| ||||
Posted in reply to yawniek | On Thursday, 7 May 2015 at 19:51:20 UTC, yawniek wrote:
>
> On Thursday, 7 May 2015 at 18:59:13 UTC, Suliman wrote:
>> 1. Do I need write "./public/" ? In examples often simply "public/"
> will work too. even "public"
> it goes trough Path struct, see:
> https://github.com/rejectedsoftware/vibe.d/blob/11578aa956a9b3b0e305d655f9668a867fdd89bd/source/vibe/inet/path.d
>
>
>> 2. What incoming parameters ("HTTPServerRequest req, HTTPServerResponse res") mean? Why I should to specify them?
>
> HTTPServerRequest contains all data that the client sends, e.g.
> headers, cookies, source ip etc.
> see:
> https://github.com/rejectedsoftware/vibe.d/blob/11578aa956a9b3b0e305d655f9668a867fdd89bd/source/vibe/http/server.d#L584
>
> HTTPServerResponse is the response you send back.
> https://github.com/rejectedsoftware/vibe.d/blob/11578aa956a9b3b0e305d655f9668a867fdd89bd/source/vibe/http/server.d#L788
>
> you woud want to set at least its body, as you do below
>
>> 3. Why code with: "res.writeBody("Hello, World!", "text/plain");"
>> and "router.get("*", serveStaticFiles("./public/"));" also work, but my variant (see code above) do not load say that page not found?
>
> what exactely does not work? please link code (ideally https://gist.github.com/ )
>
>> 4. How to specify page that I need to load, why in examples there is only link to folder like public? But what if I want to load public/foo.html?
>
> public servers public files, its more or less a static webserver which checks if a file exists and then serves that.
>
> if you want to dynamically modify content you send you need to use the templating or do your own string magic.
>
>
> generally it seems you do not fully understand the concept of how these web frameworks work. i think you should either read vibe.d's source code or more read how other such frameworks work e.g. http://www.sinatrarb.com/documentation.html has pretty good documentations and books that explain the inner workings (but mind, ruby is a dynamically typed language).
>
> then i also can recommend that you check out vibe.d's github repositories and read trough all the example projects that come with it. e.g.
> https://github.com/rejectedsoftware/vibe.d/tree/11578aa956a9b3b0e305d655f9668a867fdd89bd/examples/app_skeleton
I'd say it is a bit hard to get into vibe.d just like that. I had to work out a lot of things myself simply because it's web server technology cast in D. Most of the difficulties are not D but understanding the whole web server thing. But I have to say vibe.d is very very good once you know your way around. Maybe we should set up some tutorials with common tasks so developers can concentrate on D instead of the web.
|
May 08, 2015 Re: vibed: how to use pure HTML instead of template engine? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | On 8/05/2015 10:17 p.m., Chris wrote:
> On Thursday, 7 May 2015 at 19:51:20 UTC, yawniek wrote:
>>
>> On Thursday, 7 May 2015 at 18:59:13 UTC, Suliman wrote:
>>> 1. Do I need write "./public/" ? In examples often simply "public/"
>> will work too. even "public"
>> it goes trough Path struct, see:
>> https://github.com/rejectedsoftware/vibe.d/blob/11578aa956a9b3b0e305d655f9668a867fdd89bd/source/vibe/inet/path.d
>>
>>
>>
>>> 2. What incoming parameters ("HTTPServerRequest req,
>>> HTTPServerResponse res") mean? Why I should to specify them?
>>
>> HTTPServerRequest contains all data that the client sends, e.g.
>> headers, cookies, source ip etc.
>> see:
>> https://github.com/rejectedsoftware/vibe.d/blob/11578aa956a9b3b0e305d655f9668a867fdd89bd/source/vibe/http/server.d#L584
>>
>>
>> HTTPServerResponse is the response you send back.
>> https://github.com/rejectedsoftware/vibe.d/blob/11578aa956a9b3b0e305d655f9668a867fdd89bd/source/vibe/http/server.d#L788
>>
>>
>> you woud want to set at least its body, as you do below
>>
>>> 3. Why code with: "res.writeBody("Hello, World!", "text/plain");"
>>> and "router.get("*", serveStaticFiles("./public/"));" also work, but
>>> my variant (see code above) do not load say that page not found?
>>
>> what exactely does not work? please link code (ideally
>> https://gist.github.com/ )
>>
>>> 4. How to specify page that I need to load, why in examples there is
>>> only link to folder like public? But what if I want to load
>>> public/foo.html?
>>
>> public servers public files, its more or less a static webserver which
>> checks if a file exists and then serves that.
>>
>> if you want to dynamically modify content you send you need to use the
>> templating or do your own string magic.
>>
>>
>> generally it seems you do not fully understand the concept of how
>> these web frameworks work. i think you should either read vibe.d's
>> source code or more read how other such frameworks work e.g.
>> http://www.sinatrarb.com/documentation.html has pretty good
>> documentations and books that explain the inner workings (but mind,
>> ruby is a dynamically typed language).
>>
>> then i also can recommend that you check out vibe.d's github
>> repositories and read trough all the example projects that come with
>> it. e.g.
>> https://github.com/rejectedsoftware/vibe.d/tree/11578aa956a9b3b0e305d655f9668a867fdd89bd/examples/app_skeleton
>>
>
> I'd say it is a bit hard to get into vibe.d just like that. I had to
> work out a lot of things myself simply because it's web server
> technology cast in D. Most of the difficulties are not D but
> understanding the whole web server thing. But I have to say vibe.d is
> very very good once you know your way around. Maybe we should set up
> some tutorials with common tasks so developers can concentrate on D
> instead of the web.
I was thinking maybe a vibe.d manual using leanpub's systems. Assuming it was hosted on Github and set to be free. It would work out rather well and because you can embed other files into the book, examples can be made kinda like they are now in vibe.d's repo runnable. But also contained in the manual inline. All syntax highlighted and all.
|
Copyright © 1999-2021 by the D Language Foundation