使用C++搭建一个简单httpServer

本案例使用libhv 进行简单的使用

1. 下载libhv 源码并编译你需要使用的版本

我目前使用的是win 32 版本 并且已经上传到网盘 密码8888

2.将库文件添加到项目当中

3.进行代码简单开发示例demo下载地址 密码:8888

	#include "hv/HttpServer.h"

#include 

#pragma comment(lib,"hv.lib")

using namespace std;
//  其中的json 使用的是nlohmann 对中文的编码格式需要自行转换 否则会造成程序奔溃现象
int main() {
    HttpService router;
    // http://192.168.8.104:8080/ping
    router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
        return resp->String("pong");
        });

    // http://127.0.0.1:8080/data?para=1¶2=123
    router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
        auto para = req->GetParam("para");
        auto para2 = req->GetParam("para2");
        cout << "para: " << para << "  para2: " << para2 << endl;
        static char data[] = "0123456789";
        return resp->Data(data, 10);
        });

    // http://127.0.0.1:8080/paths
    router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
        return resp->Json(router.Paths());
        });

    // http://127.0.0.1:8080/echo
    router.POST("/echo", [](const HttpContextPtr& ctx) {
        return ctx->send(ctx->body(), ctx->type());
        });

    http_server_t server;
    server.port = 8080;
    server.service = &router;
    http_server_run(&server);
    return 0;
}

你可能感兴趣的:(软件资源,c++,开发语言,前端)