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.13k stars 92 forks source link

How to send back Html file #130

Closed noctera closed 3 years ago

noctera commented 3 years ago

I want to send a Html file to the user

router->http_get(
        "/html",
        []( auto req, auto ){
                init_resp( req->create_response() )
                        .append_header( restinio::http_field::content_type, "text/html; charset=utf-8" )
                        .set_body(restinio::sendfile( "./adminPanel/index.html" ))
                        .done();

                return restinio::request_accepted();
        } );

but the console is always telling me

[2020-11-21 17:03:56.494] ERROR: [connection:2] error while handling request: unable to openfile './adminPanel/index.html': No such file or directory

Could someone help me? The html file is located in ./adminPanel/index.html

eao197 commented 3 years ago

Hi!

Try to specify the absolute path as a parameter for sendfile. If it will work then you have to check the current directory for your process.

noctera commented 3 years ago

I got it running with the absolute path, but somehow, when specifying the path of the current directory, it does not work

noctera commented 3 years ago

Ok solved it. Somehow it worked with

"../src/adminPanel/index.html"

I had to get out of the directory and cd into it again. Strange but luckily it solved the problem. Thanks for your help :)