1

After adding all the necessary header files from Crow, I finally managed to run and get the Hello World output at my localhost port. When I try to render a webpage using mustache command with a simple HTML file, the program compiles but I only get a blank page. Please help.

main.cpp

#include <iostream>
#include "crow.h"

int main(int argc, const char * argv[]) {

crow::SimpleApp app;
crow::mustache::set_base(".");

CROW_ROUTE(app, "/")([](){
    crow::mustache::context ctx;
    auto main_page = crow::mustache::load("site.html");
    return main_page.render();
});

app.port(18080)
//    .multithreaded()
.run();
return 0;
}

site.html

<!DOCTYPE html>
<html>
 <body>
    <h1>My First Heading</h1>
    <p>My first paragraph.</p>
 </body>
</html>
Sugan
  • 15
  • 10

4 Answers4

2

Actially you can simply create folder called "template" and place there all of your htmls and then load them by name. This is official example: https://crowcpp.org/master/getting_started/a_simple_webpage/

LightError
  • 21
  • 4
1

My experience with Crow was using Docker, and I had the same issue loading static files. To load the html, you cannot use a relative path, but instead must use an absolute path. For example, I had my main.cpp file in a folder called "server" and my index.html in a folder called "static". I initially said

auto main_page = crow::mustache::load("../static/index.html"); // "../" means go to parent folder

This did not work for me but you can go ahead and try it. Instead, I used the exact directory it was stored in my Docker container. "usr/src/web/mysite/static/index.html"

NeonFire
  • 186
  • 2
  • 7
0

Alternate solution is to render the static files using another server such as Node, and add CORS headers in the responses from C++ server using Crow.

prantoran
  • 1
  • 2
0

I have a same issue. I replace load to load_unsafe.It works fine.

Auly
  • 9
  • 4