Crow 编译和环境搭建

Crow与其说是编译,倒不如说是环境搭建。Crow只需要包含头文件,所以不用编译生成lib。

Crow环境搭建

  • boost(可以不编译boost,只需要boost头文件即可)
  • asio (可以不编译,直接包含头文件。不能直接使用boost的asio,boost的asio存在命名空间)
  • zlib (可选)用于web压缩,如果不使用压缩,可以不包含。开启需要定义宏:CROW_ENABLE_COMPRESSION
boost环境可参考:boost 编译
asio环境

下载:https://think-async.com/Asio/

编译:需要依赖boost(可以不编译boost)

  • 打开目录 ,找到Makefile.msc Makefile.mgw 修改BOOSTDIR指向自己的boost目录BOOSTDIR = C:\\Boost\\include\\boost_1_73
  • 打开VS命令行工具,执行nmake -f Makefile.msc编译完成
Hellow Word
  • 1.包含Crow头文件,boost头文件,asio头文件
    在这里插入图片描述

  • 2 开始使用Crow

crow::SimpleApp app;

CROW_ROUTE(app, "/123")
        ([&](crow::request& req, crow::response &resp) {
        resp.body = "123";
        resp.end();
            });
   
   app.port(18080)
        .multithreaded().run();         
扩展:Crow加载静态资源

将静态资源放在运行目录statics,此时通过web访问http://127.0.0.1:18080/statics/index.html,将自动加载网页所需要的cs,js,png等资源。

 //处理所有未被处理的路径
	CROW_CATCHALL_ROUTE(app)([&](const crow::request& req, crow::response& resp) {
        //如果访问path以/statics/开始,
        if (_strnicmp(req.url.c_str(), "/statics/", 9) == 0)
        {
            //拼接静态文件路径,不要以绝对路径,内部会处理:保证web服务的安全
            std::string path = req.url.substr(9);
            std::string file_path = std::string("statics/") + path;
            if (_access(file_path.c_str(), 00) == 0)
            {
                resp.set_static_file_info(file_path);
                resp.code = crow::status::OK;
                resp.end();
            }

        }
		});

结果展示:
在这里插入图片描述

强烈建议hv(不支持压缩),一句代码搞定
HttpService router;
// statics为urlpath
// smart-yi-ui为当前服务静态资源所在目录
router.Static("/statics", "smart-yi-ui");

http://127.0.0.1:18080/statics/index.html

相关推荐

  1. 基于 Docker 交叉编译环境

    2024-03-13 18:28:01       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-13 18:28:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-13 18:28:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-13 18:28:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-13 18:28:01       18 阅读

热门阅读

  1. vue2和vue3的区别?

    2024-03-13 18:28:01       25 阅读
  2. data engineer

    2024-03-13 18:28:01       17 阅读
  3. 对盒子模型的理解(box-sizing)

    2024-03-13 18:28:01       22 阅读
  4. 【子串】76. 最小覆盖子串【困难】

    2024-03-13 18:28:01       18 阅读