[Webserver]httplib webserver(MacOS)

👉🏻 C++의 httplib를 사용한 매우 간단한 웹서버 입니다.
This is a very simple web server using C++’s httplib.

👉🏻 사용방법은 nodejs express서버와 매우 유사합니다.
The usage is very similar to the Node.js Express server.

👉🏻코드 / Code

✔️ httplib.h

— 아래의 명령어로 다운로드합니다.
Download using the command below.

curl -L https://raw.githubusercontent.com/yhirose/cpp-httplib/master/httplib.h -o httplib.h

✔️ httplib_server.cpp

#include "httplib.h"
#include <iostream>

int main()
{
  httplib::Server svr;

  svr.Get("/",[](const httplib::Request &req, httplib::Response &res){
   res.set_content("Hello from cpp-httplib!","text/plain");
  });

  svr.Get("/hi", [](const httplib::Request &req, httplib::Response &res) {
    res.set_content("Hi! from cpp-httplib!", "text/plain");
  });

  std::cout << "cpp-httplib server listening on http://localhost:8080" << std::endl;
  svr.listen("0.0.0.0", 8080);

  return 0;
}

👉🏻 컴파일 / Compiling

g++ -o httplib_server httplib_server.cpp -std=c++11 -pthread

👉🏻실행 / run

✔️ 서버 실행 / Run server

./httplib_server

✔️ 브라우저에서 서버 접속 / Connect to server from browser

Leave a Reply