Vix.cpp v2.6.0 is here Read the blog
Skip to content

Hello App

This example is the smallest useful Vix HTTP application.

It shows the modern vix::App style:

txt
create an App
register routes
write responses
run the server
test with curl
1
2
3
4
5

Use this example first before moving to JSON APIs, middleware, authentication, static files, or cache.

Source

Create a file:

txt
hello_app.cpp
1

Add this code:

cpp
#include <vix.hpp>

using namespace vix;

int main()
{
  App app;

  app.get("/", [](Request &, Response &res)
  {
    res.text("Hello from Vix");
  });

  app.get("/health", [](Request &, Response &res)
  {
    res.json({
      "ok", true,
      "service", "hello-app"
    });
  });

  app.get("/hello", [](Request &req, Response &res)
  {
    const std::string name = req.query_value("name", "Vix");

    res.json({
      "message", "Hello, " + name
    });
  });

  app.run(8080);
  return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

Run it

bash
vix run hello_app.cpp
1

The server listens on:

txt
http://127.0.0.1:8080
1

Test the home route

bash
curl -i http://127.0.0.1:8080/
1

Expected status:

txt
HTTP/1.1 200 OK
1

Expected body:

txt
Hello from Vix
1

This is the simplest route shape:

cpp
app.get("/", [](Request &, Response &res)
{
  res.text("Hello from Vix");
});
1
2
3
4

The route receives a request and a response.

It writes text to the response.

Test the health route

bash
curl -i http://127.0.0.1:8080/health
1

Expected body shape:

json
{
  "ok": true,
  "service": "hello-app"
}
1
2
3
4

The route writes JSON:

cpp
app.get("/health", [](Request &, Response &res)
{
  res.json({
    "ok", true,
    "service", "hello-app"
  });
});
1
2
3
4
5
6
7

This is useful for simple health checks.

Test a query parameter

bash
curl -i "http://127.0.0.1:8080/hello?name=Ada"
1

Expected body shape:

json
{
  "message": "Hello, Ada"
}
1
2
3

The route reads a query value:

cpp
const std::string name = req.query_value("name", "Vix");
1

If the query parameter is missing, the default value is used.

bash
curl -i http://127.0.0.1:8080/hello
1

Expected body shape:

json
{
  "message": "Hello, Vix"
}
1
2
3

What this example teaches

This example introduces the base shape of a Vix application.

cpp
#include <vix.hpp>

using namespace vix;

int main()
{
  App app;

  app.get("/", [](Request &, Response &res)
  {
    res.text("Hello from Vix");
  });

  app.run(8080);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

The important parts are:

PartPurpose
#include <vix.hpp>Includes the main Vix API
App app;Creates the HTTP application
app.get(...)Registers a GET route
Request &Gives access to request data
Response &Writes the response
app.run(8080)Starts the server on port 8080

Route handlers

A route handler usually receives:

cpp
Request &req
Response &res
1
2

Example:

cpp
app.get("/hello", [](Request &req, Response &res)
{
  const std::string name = req.query_value("name", "Vix");

  res.json({
    "message", "Hello, " + name
  });
});
1
2
3
4
5
6
7
8

Use Request to read:

txt
method
path
headers
query parameters
body
typed state
1
2
3
4
5
6

Use Response to write:

txt
status
headers
text
JSON
files
1
2
3
4
5

Text responses

Use res.text(...) for plain text:

cpp
res.text("Hello from Vix");
1

You can also set a status first:

cpp
res.status(200).text("OK");
1

JSON responses

Use res.json(...) for JSON:

cpp
res.json({
  "ok", true
});
1
2
3

For created resources:

cpp
res.status(201).json({
  "ok", true,
  "created", true
});
1
2
3
4

For errors:

cpp
res.status(404).json({
  "ok", false,
  "error", "Not found"
});
1
2
3
4

Status codes

A response can set the HTTP status explicitly.

cpp
app.get("/missing", [](Request &, Response &res)
{
  res.status(404).json({
    "ok", false,
    "error", "Not found"
  });
});
1
2
3
4
5
6
7

Test:

bash
curl -i http://127.0.0.1:8080/missing
1

Expected status:

txt
HTTP/1.1 404 Not Found
1

Add another route

You can add more routes before app.run(...).

cpp
app.get("/version", [](Request &, Response &res)
{
  res.json({
    "name", "hello-app",
    "version", "1.0.0"
  });
});
1
2
3
4
5
6
7

Routes are registered during startup.

After that, app.run(8080) starts the server.

Keep main() simple

For tiny examples, it is fine to register routes directly in main().

For bigger applications, move route registration into a function.

cpp
#include <vix.hpp>

using namespace vix;

static void register_routes(App &app)
{
  app.get("/", [](Request &, Response &res)
  {
    res.text("Hello from Vix");
  });

  app.get("/health", [](Request &, Response &res)
  {
    res.json({
      "ok", true
    });
  });
}

int main()
{
  App app;

  register_routes(app);

  app.run(8080);
  return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

This keeps the startup structure clear:

txt
create app
register routes
run server
1
2
3

Complete version with route function

cpp
#include <vix.hpp>

using namespace vix;

static void register_routes(App &app)
{
  app.get("/", [](Request &, Response &res)
  {
    res.text("Hello from Vix");
  });

  app.get("/health", [](Request &, Response &res)
  {
    res.json({
      "ok", true,
      "service", "hello-app"
    });
  });

  app.get("/hello", [](Request &req, Response &res)
  {
    const std::string name = req.query_value("name", "Vix");

    res.json({
      "message", "Hello, " + name
    });
  });
}

int main()
{
  App app;

  register_routes(app);

  app.run(8080);
  return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

This is the version you should prefer when an example starts to grow.

Next steps

Continue with:

  1. JSON API
  2. Middleware API
  3. API Key Auth

Hello App teaches the base application shape.

The next examples add request parsing, middleware, and route protection.

Released under the MIT License.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.