Logo
Build your web server in C++ with Crow
Build your web server in C++ with Crow

Great you are back! Have you ever created a web server using a microframework before, like Flask or Express, and served a simple webpage or tried out some query parameters in the URL?

So, why not put your C++ skills to good use and build a simple web server in C++? This blog is specifically targeted towards Windows users; others can refer to the docs for installation.


Prerequisites

First and foremost, get ready with your tools:

  • Visual Studio – An IDE for Windows application development by Microsoft. Make sure to include packages for C++ development during installation.
  • vcpkg – Package manager for C++ just like npm or pip.

Installing Crow

  • Now let’s begin with installing Crow as a package:
vcpkg install crow
  • Next, link vcpkg globally so you don’t have to bother with CMake. First, go to the root directory where vcpkg was installed (for example, C:\Users\aditya\vcpkg):
cd <Your file path to vcpkg>
  • Now to link all the installations, run:
vcpkg integrate install

What does this command do?

  • Modifies Visual Studio’s MSBuild Targets: It tells Visual Studio to search for include directories and library paths within your vcpkg installation directory when building any C++ project.
  • Automatic Package Linking: After running this command, when you build a Visual Studio project that requires a vcpkg package, Visual Studio will automatically:
    • Find the header files in the vcpkg/installed/<triplet>/include directory.
    • Find the library files (.lib, .dll) in the vcpkg/installed/<triplet>/lib and vcpkg/installed/<triplet>/bin directories.
    • Link the necessary libraries during the linking stage of your build process.

Setting Up Your Project

Now we are ready to build our project!

  1. Open Visual Studio and create a new empty project. Name your project as you wish and leave the rest as default.
  2. In Solution Explorer, right-click on Source Files > Add > New Item > main.cpp.

Inside main.cpp, add the following code to set up a basic server with three different routes and features of Crow:

#include <crow.h>

int main() {
    crow::SimpleApp app;
    CROW_ROUTE(app, "/")([]() {
        std::string res =  "Crow server is running ";
        return res;
    });
    CROW_ROUTE(app, "/<string>")([](std::string name) {
        auto page = crow::mustache::load("fancypage.html");
        crow::mustache::context ctx({ {"person", name} });
        return page.render(ctx);
    });
    CROW_ROUTE(app, "/modulo/<int>/<int>")([](int a, int b) {
        std::string res = "Answer of " + std::to_string(a) + " modulo " + std::to_string(b) + " is " + std::to_string(a % b);
        return res;
    });
    app.port(1808).multithreaded().run();
}

This code sets up all the routes and the ability to run the server on port 1808.

Don’t worry about the weird syntax of lambdas in C++ — you’ll get comfortable with them eventually!


Adding a Template

Now let’s add the fancypage.html template.

  1. In Solution Explorer, click on your project name and click Open in File Explorer.
  2. Create a folder named templates and inside it, create a file called fancypage.html:
<!DOCTYPE html>
<html>
<body style="background-color:darkslategrey">
    <div style="display:flex; align-content:center">
        <h1 style="color:navajowhite;">Hello, {{person}}!</h1>
    </div>
</body>
</html>

Running Your Project

Click the green play icon at the top of the editor to build and run the code.
You can access the page in your browser at http://localhost:1808.

The routes will work like this:

  • / — Shows "Crow server is running"
  • /yourname — Renders the template with your name
  • /modulo/10/3 — Shows the result of 10 modulo 3

I hope you found this blog helpful and can build your Crow project without any problems. If you need help setting it up, feel free to contact me.

Enjoy building. Have a good day!