0

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.

Auly
  • 9
  • 4
  • You mention `container path`? Are you running this code in a container? If yes, show the build command and `Dockerfile`. Your problem is not with the `crow` framework. Your problem is either a missing file or an incorrect build. – John Hanley May 26 '23 at 17:04
  • Yes, I run it. Dockerfile:FROM hello_crow WORKDIR /usr/src/cppweb/hello_crow/build CMD ["./hello_crow"]. I think Dockerfile has no error. Because when the page is created with string, it displayed on localhost:8080. Then I just made `public/index.html` and edited main.cpp. – Auly May 27 '23 at 07:12
  • And I use cmake build it. – Auly May 27 '23 at 07:22
  • Please put details, such as Dockerfile, in your post and not as comments. Include the directory layout. – John Hanley May 27 '23 at 07:42
  • Note: If you are running `crow` from a container, you cannot use Windows-style paths. You must reference files that are inside the container. For example, this path does not exist: `C:/Users/Auly/Desktop`. – John Hanley May 27 '23 at 07:43
  • You're right. I run it before on windows. I just run it onWSL and use container path. It works. But I dont't why the video ("https://www.linkedin.com/learning/web-servers-and-apis-using-c-plus-plus/creating-html-pages?autoplay=true&resume=false") can do it. – Auly May 27 '23 at 09:05
  • When I use relative path, it also does not work. Only container path is ok. – Auly May 27 '23 at 09:19
  • Try `"/usr/src/cppweb/hello_crow/public/index.html"` The current directory when you execute `hello_crow` is `/usr/src/cppweb/hello_crow/build`. That means the location of index.html is `../public/index.html`. If that does not work, then something else is wrong. Note: include your `CMakeLists.txt` and the `Dockerfile` that built the hello_crow image in your post. Clearly show how each container is built. You are leaving out important details. – John Hanley May 27 '23 at 09:28
  • I have another question to ask you. when I run "docker build -t hello_crow ." in hello_crow directory. The message show 2 lines command executed, but Dockerfile have 3 lines. Why ? – Auly May 27 '23 at 15:06
  • I am not sure. The `CMD` line is executed when the container runs. – John Hanley May 27 '23 at 18:06
  • @JohnHanley you inspired me so much. I have another question about this project. Please your help. [mongodb-collections-data-display-blank-on-webpage](https://stackoverflow.com/questions/76371596/mongodb-collections-data-display-blank-on-webpage) – Auly May 31 '23 at 11:09

1 Answers1

0

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.