Stiffstream / restinio

Cross-platform, efficient, customizable, and robust asynchronous HTTP(S)/WebSocket server C++ library with the right balance between performance and ease of use
Other
1.15k stars 93 forks source link

sending HTML best practice #125

Open jschmitt-mscs opened 4 years ago

jschmitt-mscs commented 4 years ago

Is the best way to send HTML via Restinio as a file or written out as in the examples? The below is working for me. What would be the best way to pass the parameter "test" and to make an SQL query and have the data returned into the browser?

router->http_get("/index/:test",
    [](auto req, auto params) {
        return
            init_resp(req->create_response())
            .append_header(restinio::http_field::content_type, "text/html; charset=utf-8")
            .set_body(restinio::sendfile("C\\:..\html1.htm"))
            .done();
    });
eao197 commented 4 years ago

Hi!

I think "the best way" depends on your case. If you have to return just static HTML content then sendfile would be a better choice. But if you have to integrate some dynamic data into your HTML then you have to use set_body or append_body methods of response builder (maybe with help of some HTML template generators like jinja2cpp (a list of C++ HTML template engines can be found here)).

An example of performing database requests during the request processing can be found here.

jschmitt-mscs commented 4 years ago

Thanks for the help and recommendations!