redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

How to respond with an HTML #48

Closed cgarciae closed 9 years ago

cgarciae commented 9 years ago

Is there an easy way to respond with an HTML file in the web folder? I most of the example I see respond json or plain text, but there must be an easy way to respond the start page.

digitalfiz commented 9 years ago

Check out the shelf middlwares for static files:

https://github.com/luizmineo/redstone.dart/wiki/Shelf-Middlewares

The second example will allow you to do what you want.

platelk commented 9 years ago

Or you can just return a file

@app.Route("/yourroute") myRouteFunc() => new File("your_file_path.html");

cgarciae commented 9 years ago

@platelk your solutions is very clean. This should be one of the first examples of the documentation.

platelk commented 9 years ago

Thanks ^^

After 2month of working with redstone, i think that the documentation should me improved. or at least take care of new elements present in the code. Maybe i will contribute :)

digitalfiz commented 9 years ago

Well technically if @platelk's solution is what you needed it really had nothing to do with redstone, you just needed to know how to read a file in Dart.

I thought you where looking for a redstone solution to serve static files as is (http://host.com/somefile.html). Not outputting html content on certain routes (http://host.com/someroute).

If you're looking to just serve html files from the web folder like your OP says then using the static shelf would be a cleaner solution, otherwise you need to make a route for every file you want to serve.

A static shelf pointed at web would route things like so automatically:

web/file1.html => http://host.com/file1.html
web/subfolder/file2.html => http://host.com/subfolder/file2.html
web/assets/images/animated.png => http://host.com/assets/images/animated.png

If you're just trying to do something like routing the index (root of your site) to an index.html I would still recommend using the static shelf and doing something like this so you're still only using one mechanism of serving static files:

@app.Route("/")
indexPage() => app.redirect("/index.html");
cgarciae commented 9 years ago

@digitalfiz I haven't checked both solutions. I just need to serve index.html on route /, but @platelk seems easier and I am new to redstone.

luizmineo commented 9 years ago

@cgarciae using the shelf_static middleware is the preferable way to handle static files, since it will properly configure the response headers for you, but you are free to implement it any way you like.

@platelk contributions are always welcome :) To improve the documentation, you can just update our wiki, so I can easily sync it with the website.

cgarciae commented 9 years ago

@luizmineo I am new te server-side stuff, what is the benefit of configuring the response headers?

luizmineo commented 9 years ago

@cgarciae For example, shelf_static can handle the if-modified-since header, sent by the browser. If the file wasn't changed since the last request, the server just respond with the 304 (not modified) status code, instead of sending the file. Also, shelf_static is more convenient if you have to serve an entire directory.

cgarciae commented 9 years ago

Thanks! Will keep in mind for the future :)

karan commented 9 years ago

I would suggest adding an example of rendering a static file in the Routing wiki page.

luizmineo commented 9 years ago

@karan Thanks. I've opened a new issue for this (issue #60).

avstudios commented 9 years ago

I'm trying to build Single Page App. The second example from https://github.com/redstone-dart/redstone/wiki/Shelf-Middlewares page works fine so far. I'm not only sure how to serve files from root directory if I use links like /page1, /page2 etc...

web/index.html => http://host.com
web/index.html => http://host.com/page1
web/assets/images/animated.png => http://host.com/page1/assets/images/animated.png

I'm guessing I need to write something like the bellow code but I'm not sure what should be in the childPage function body.

@app.Route("/.*")
childPage() =>  ?;
cgarciae commented 9 years ago

Your code above is not needed. The shelf static middlewear will match files from the directory you asigned.

For example, if you set web as the parent folder and index.html as the default asset, given this folder structure

- web
  index.html
  - images
    icon1.png
    icon2.png

The route to icon1.png is http://host/images/icon1.png. Its somewhat intuitive.

avstudios commented 9 years ago

@cgarciae maybe I was not clear enough. I set 'web' as root folder and when I type in the browser:

http://host.com

index.html and all assets are loaded and displayed correctly. This is my home page.

I have also have few subpages like page1, page2, etc.. and when I type:

http://host.com/page1

I also would like to load index.html and the assets from root folder. On the client side I'm using route_hierarchical lib so I can display a content for page1 in index.html

At the moment I see only error page with bellow text

404 - NOT FOUND
Resource: /page1

I hope this explains better what I want to achieve.