c++ http第一个服务

c++ http第一个服务

一、下载相关依赖:这是一个git开源项目 代码仓地址

二、演示代码,编译参数:g++ test.cpp -I/**** -lpthread

c++ 复制代码
#include <httplib.h>
using namespace httplib;

void wuhan(const Request &req, Response &res)
{
    printf("httplib server recv a req: %s\n ", req.path.c_str() );
    res.set_content("<html>  \
                        <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"> \
                        <h1> 武汉, 加油!</h1></html>",
                    "text/html");
    res.status = 200;
}

int main(void)
{
    using namespace httplib;

    Server svr;
    svr.set_base_dir("./");

/// Get
svr.Get("/wuhan", wuhan);


    svr.Get("/hi", [](const Request& req, Response& res) {
        res.set_content("Hello World!", "text/plain");
    });

    svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
        auto numbers = req.matches[1];
        res.set_content(numbers, "text/plain");
    });

    svr.Get("/body-header-param", [](const Request& req, Response& res) {
        if (req.has_header("Content-Length")) {
            auto val = req.get_header_value("Content-Length");
        }
        if (req.has_param("key")) {
            auto val = req.get_param_value("key");
        }
        res.set_content(req.body, "text/plain");
    });

    svr.Get("/stop", [&](const Request& req, Response& res) {
        svr.stop();
    });

    /// listen
    svr.listen("localhost", 1234);
}

三、访问网址http://localhost:1234/hi

相关推荐
70asunflower4 分钟前
CUDA基础知识巩固检验练习题【附有参考答案】(8)
c++·人工智能·cuda
会编程的土豆7 分钟前
【影院票务管理系统】
开发语言
tankeven12 分钟前
HJ134 1or0
c++·算法
笨笨马甲20 分钟前
Qt 嵌入式开发快速搭建交叉编译环境
开发语言·qt
春日见21 分钟前
Matlab快速入门 基础语法教学
java·开发语言·驱动开发·matlab·docker·计算机外设
张人玉21 分钟前
C# 中的 MVC、MVP、MVVM 模式详解
开发语言·c#·mvc·mvvm·mvp
dgfhf22 分钟前
高性能计算资源调度
开发语言·c++·算法
Lhan.zzZ23 分钟前
Qt绘图探秘:如何避免多QPainter冲突引发的程序崩溃
开发语言·c++·qt
Ralph_Y27 分钟前
C++:迭代器失效
开发语言·c++
smart margin29 分钟前
Python安装教程
开发语言·python