[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