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

相关推荐
紫荆鱼4 小时前
设计模式-状态模式(State)
c++·后端·设计模式·状态模式
深思慎考4 小时前
微服务即时通讯系统(服务端)——Speech 语音模块开发(2)
linux·c++·微服务·云原生·架构·语音识别·聊天室项目
「QT(C++)开发工程师」4 小时前
【LUA教程】LUA脚本语言中文教程.PDF
开发语言·pdf·lua
程序定小飞4 小时前
基于springboot的民宿在线预定平台开发与设计
java·开发语言·spring boot·后端·spring
沐怡旸5 小时前
【穿越Effective C++】条款7:为多态基类声明virtual析构函数——C++多态资源管理的基石
c++·面试
天天进步20155 小时前
Python全栈项目--基于计算机视觉的车牌识别系统
开发语言·python·计算机视觉
Algo-hx5 小时前
C++编程基础(五):字符数组和字符串
开发语言·c++
无敌最俊朗@5 小时前
C++ STL中 std::list 的高频面试题与答案
开发语言·c++·list
星光一影5 小时前
Java医院管理系统HIS源码带小程序和安装教程
java·开发语言·小程序
敲代码的瓦龙5 小时前
C语言?大小端!!!
c语言·开发语言·c++·1024程序员节