Starting a HTTP Server

Initalize the class

Note

This guide expects that you have already included the httpServer header.
I included my header like this:
#include "../headers/server.hpp"

Lets start by creating the httpServer class object. The initializer takes only one paramater, that is the port number.

int main() {
    http_server httpServer(3000);
    return 0;
}

I’ve started my server on port 3000, but you can start your server on any port (including port 80)

Warning

This HTTP Server does not https. If you choose to run the server on port 443, you may find that the web browsers (such as Chrome) won’t open the page.

Start the server

If you complile and run the program, it will exit without the actually starting the http_server. This is now what we need to do.

Add this to your code

httpServer.start();

Yay! The server has started. If you go to http://localhost:3000 you will discover a welcome message.

Warning

Anything that you type after the start function will not be run until the server closes! The server takes over the thread.