-
Notifications
You must be signed in to change notification settings - Fork 10
Creating a New Server
To create a new server, you should create a new object of Server
class. The constructor of Server
class has an optional arguments:
-
int port
: specifies on which port server will be listen
After that, You should define some Route
s to server and add a POST
and/or GET
handler to every one of them. You can do this by calling get(std::string path, RequestHandler *handler)
and post(std::string path, RequestHandler *handler)
. Moreover, you can add a handler for not found errors by setNotFoundErrPage(std::string)
, which its argument is address of .html file which should be served when a request with an invalid address is received.
At last, you should call run()
method to start server.
Server will throw a Server::Exception
object on failure.
Your main
can be like:
int main(int argc, char **argv) {
try {
Server server(argc > 1 ? atoi(argv[1]) : 5000);
server.setNotFoundErrPage("static/404.html");
server.post("/login", new LoginHandler()); // get data from client-side
server.get("/rand", new RandomNumberHandler()); // serve dynamic page
server.get("/home.png", new ShowImage("static/home.png")); // serve static image
server.get("/", new ShowPage("static/home.html")); // serve static page
server.run();
} catch (Server::Exception e) {
cerr << e.getMessage() << endl;
}
}
AP HTTP
University of Tehran / Advanced Programming
- Quick How-to & Examples
- Creating a New Server
-
Request Handlers
- Serving Static Files
- Serving Dynamic Files
- Getting Data from Client-side
- Redirection
- Serving Template Files
- Template Files
-
Request
- Request-Line
- Query
- Haeder
- Body
-
Response
- Status-Line
- Header
- Body
- Session
- Utilities
- Compile and Run