Your First Application
This page shows how you can get started with a simple hello world application.
1. Include¶
Starting with an empty main.cpp
file, first add #include "crow.h"
or #include "crow_all.h"
if you're using the single header file.
Note
If you are using version v0.3, then you have to put #define CROW_MAIN
at the top of one and only one source file.
2. App declaration¶
Next Create a main()
and declare a crow::SimpleApp
inside, your code should look like this
int main()
{
crow::SimpleApp app;
}
3. Adding routes¶
Once you have your app, the next step is to add routes (or endpoints). You can do so with the CROW_ROUTE
macro.
CROW_ROUTE(app, "/")([](){
return "Hello world";
});
4. Running the app¶
Once you're happy with how you defined all your routes, you're going to want to instruct Crow to run your app. This is done using the run()
method.
app.port(18080).multithreaded().run();
port()
and multithreaded()
methods aren't needed, though not using port()
will cause the default port (80
) to be used.Putting it all together¶
Once you've followed all the steps above, your code should look similar to this
main.cpp | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
You then need to compile your code on your Linux, MacOS, or Windows machine
After building your .cpp
file and running the resulting executable, you should be able to access your endpoint at http://localhost:18080. Opening this URL in your browser will show a white screen with "Hello world" typed on it.