How to fix 'Not found' when display HTML with Crow?

250 Views Asked by At

I use windows10.I use crow, when display page with string,it works. But when display page with a html file, return "Not found". main.cpp

#include "crow_all.h"
using namespace std;
using namespace crow;

int main(int argc, char* argv[]) 
{
    crow::SimpleApp app;

    CROW_ROUTE(app, "/")
    ([](const crow::request &req, crow::response &res)
    {
        ifstream in("file:///C://Users//Auly//Desktop//cppweb//hello_crow//public//index.html", ifstream::in);
        if (in)
        {
            ostringstream contents;
            contents << in.rdbuf();
            in.close();
            res.write(contents.str());
        } 
        else 
        {
            res.write("Not found");
        } 
        res.end();
    });

    char* port = getenv("PORT");
    uint16_t iPort = static_cast<uint16_t>(port != NULL? stoi(port): 18080);
    cout << "PORT = " << iPort << endl;
    app.port(iPort).multithreaded().run();
}

index.html

#include "crow_all.h"
using namespace std;
using namespace crow;

int main(int argc, char* argv[]) 
{
    crow::SimpleApp app;
       
    CROW_ROUTE(app, "/")
    ([](const crow::request& req, crow::response& res)
    {
        ifstream in("../public/index.html", ifstream::in);
        if (in)
        {
            ostringstream contents;
            contents << in.rdbuf();
            in.close();
            res.write(contents.str());
        } 
        else 
        {
            res.write("Not found");
        }
        res.end();
    });

    char* port = getenv("PORT");
    uint16_t iPort = static_cast<uint16_t>(port != NULL? stoi(port): 18080);
    cout << "PORT = " << iPort << endl;
    app.port(iPort).multithreaded().run();
}

hello_crow/Dockerfile

FROM hello_crow
WORKDIR /usr/src/cppweb/hello_crow/build
CMD ["./hello_crow"]
  • cppweb
    • cppbox
      • Dockerfile
    • hello_crow
      • build
      • public
        • index.html
      • crow_all.h
      • main.cpp
      • Dockerfile

In terminal:

/usr/src/cppweb/hello_crow/build:make
docker run -v /mnt/c/Users/Auly/Desktop/cppweb:/usr/src/cppweb -p 8080:8080 -e PORT=8080 cppbox:latest /usr/src/cppweb/hello_crow/build/hello_crow

It should be: docker run -v /mnt/c/Users/Auly/Desktop/cppweb:/usr/src/cppweb -p 8080:8080 -e PORT=8080 hello_crow:latest /usr/src/cppweb/hello_crow/build/hello_crow Then it works when use "../public/index.html".
I try relative path:"../public/index.html", absolute path:"C:/Users/Auly/Desktop/cppweb/hello_crow/public/index.html" and container path:"usr/src/cppweb/hello_crow/public/index.html". All of them return Not found. How can I fix it? solution: Run it on WSL and set path "usr/src/cppweb/hello_crow/public/index.html".
when I run "docker build -t hello_crow ." in hello_crow directory. I get this message:

[+] Building 0.9s (6/6) FINISHED
 => [internal] load build definition from Dockerfile                                                                            0.2s
 => => transferring dockerfile: 114B                                                                                            0.0s
 => [internal] load .dockerignore                                                                                               0.2s
 => => transferring context: 2B                                                                                                 0.0s
 => [internal] load metadata for docker.io/library/hello_crow:latest                                                            0.0s
 => [1/2] FROM docker.io/library/hello_crow                                                                                     0.5s
 => [2/2] WORKDIR /usr/src/cppweb/hello_crow/build                                                                              0.1s
 => exporting to image                                                                                                          0.1s
 => => exporting layers                                                                                                         0.1s
 => => writing image sha256:0baf55adb5f5e184956c1500b7e9c80c20f410a103dc68b984f9ec0f73da4f6e                                    0.0s
 => => naming to docker.io/library/hello_crow

Why it show [2/2] lines command? Dockerfile have 3 lines.

1

There are 1 best solutions below

0
Farook Al-Sammarraie On

In your case it would be best to use Mustache to render html files instead of ifstream.

for the file not found issue it seems John Hanley's comment should solve it.