Simple Web Server

Cpp

#include "crow.h"

int main()
{
    crow::SimpleApp app;

    CROW_ROUTE(app, "/")([](){
        return "Hello world";
    });

    app.port(8000).multithreaded().run();
}

Go

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello world")
	})

	http.ListenAndServe(":8000", nil)
}

What This Code Does

The goal is to describe a basic web-server that responds with the string "Hello, World" to the root path of the server (/).

What's the Same

Both solutions define a Sinatra/Flask style web-server where a path is matched to a simple lambda to handle the request. Both examples also "block" when spawning the web-service.

What's Different

C++, as of C++-17, does not have any standard networking features and thus no standard libraries for creating web-servers. Crow is a simple 3rd-party framework in the style of Sinatra/Flask for creating simple web apps. Other frameworks such as Boost Beast or Cutelyst also exist to serve this need.

It is worth noting that networking in the standard library is coming via the Networking TS. This TS has been published, which essentially means these features are available in a experimental/beta mode as people try them out allowing for any minor adjustments before being "written in stone." You can find the status for all specifications on the ISO CPP website.

Asynchronous networking I/O can also be achieved today with Boost.Asio, which currently follows the Networking TS in it's implementation.

Fork me on GitHub