c++ 如何修复'未找到'当显示HTML与乌鸦?

vnzz0bqm  于 2023-07-01  发布在  其他
关注(0)|答案(1)|浏览(128)

我用windows10.我用crow,当显示字符串页面时,它工作。但是当显示带有html文件的页面时,返回“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
  • 建造
  • 公众
  • index.html
  • crow_all.h
  • main.cpp
  • Dockerfile

在终端:

/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

它应该是: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然后使用“../public/index.html”就可以了。
我尝试相对路径:"../public/index.html",绝对路径:"C:/Users/Auly/Desktop/cppweb/hello_crow/public/index.html"和容器路径:"usr/src/cppweb/hello_crow/public/index.html".全部返回Not found.我该怎么解决?解决方案:在WSL上运行它并设置路径“usr/src/cppweb/hello_crow/public/index.html”。
当我在hello_crow目录中运行“docker build -t hello_crow .”时。我收到这样的信息:

[+] 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

为什么显示[2/2]行命令?Dockerfile有3行。

6bc51xsx

6bc51xsx1#

在你的例子中,最好使用Mustache来渲染html文件,而不是ifstream。
对于找不到文件的问题,似乎John汉利的评论应该可以解决它。

相关问题