linux c++服务器,由于c++语法本身繁琐,大多数的cpp服务器都不能像node服务器一样能快速上手,因此,打算做一个仿nodejs的express框架风格的c++服务器,懂一点点c++就能快速上手部署
✅大文件使用异步IO(io_uring),防止阻塞
✅支持websocket服务器推送
✅仿nodejs的express框架语法,上手简单
✅支持get、post,以及静态文件
基于cmake,需要先安装liburing(异步io封装)与openssl:sudo apt-get install libssl-dev
cmake -B build
cmake --build build
./build/test_cpp_server
#include<cpp_server/Server.h>
#include<stdio.h>
int main(){
Server app;
// 监听端口
app.listen(8000, []{
printf("监听在8000端口\n");
}, 4);
// get请求
app.get("/", [](Req &req, Res &res){
printf("name: %s\n", req.kv_data.at("name").c_str()); // 获取get参数
res.send("hello get");
});
app.run();
}
app.post("/", [](Req &req, Res &res){
printf("name: %s\n", req.kv_data.at("name").c_str()); // 获取post参数
res.send("hello post");
});
app.ws("/websocket", [](WSClient* client){
client->on_connect([](WSClient* _client){
_client->send("hello socket");
});
client->on_get([](string data,WSClient* _client){
_client->send("i get " + data);
});
});
app.static_file("/static", "/var/www/static");
// 访问127.0.0.1/static/xxx 就是访问 /var/www/static/xxx