Examples
This section contains practical Vix examples built around the current vix::App model.
The goal is not to show every feature at once.
The goal is to give you small, modern, runnable examples that make sense with the current Vix API.
Each example focuses on one real backend need:
start an HTTP app
build a JSON API
protect routes with middleware
serve static files
cache dynamic GET responses
prepare a production-style bootstrap2
3
4
5
6
What these examples are
These examples are written for the modern Vix application style:
#include <vix.hpp>
using namespace vix;
int main()
{
App app;
app.get("/", [](Request &, Response &res)
{
res.text("Hello from Vix");
});
app.run(8080);
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
When middleware is needed, examples use:
#include <vix/middleware.hpp>For Vix.cpp v2.6.2 and newer, this is enough:
#include <vix.hpp>
#include <vix/middleware.hpp>2
For Vix.cpp v2.6.0 and v2.6.1, some App middleware integration headers may need to be included explicitly:
#include <vix.hpp>
#include <vix/middleware.hpp>
#include <vix/middleware/app/adapter.hpp>
#include <vix/middleware/app/app_middleware.hpp>
#include <vix/middleware/app/http_cache.hpp>
#include <vix/middleware/app/presets.hpp>2
3
4
5
6
7
Use the shorter include form when your project is on v2.6.2 or newer.
What these examples are not
These examples are not old Vix v1 snippets.
They are not historical demos.
They are not random API fragments.
They should be:
current
small
runnable
easy to copy
easy to test with curl
aligned with vix::App
clear about what feature they demonstrate2
3
4
5
6
7
If an example depends on an unstable API, it should not be added here yet.
A wrong example is worse than no example.
Recommended reading order
Start here:
This order moves from simple to realistic.
Hello App
minimal server
JSON API
request body parsing and validation
Middleware API
security, CORS, rate limit, body limit, JSON parser
API Key Auth
protected route
Static Site
public files, index, SPA fallback, Cache-Control
HTTP Cache
dynamic GET response cache
Production Bootstrap
config-driven App setup2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Run an example
Most examples can be run with:
vix run examples/<file>.cppFor example:
vix run examples/hello_app.cppThen test with curl:
curl -i http://127.0.0.1:8080/Some documentation pages show the full source inline.
You can copy the code into a local .cpp file and run it with vix run.
Example: minimal App
#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
});
});
app.run(8080);
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Run:
vix run hello_app.cppTest:
curl -i http://127.0.0.1:8080/
curl -i http://127.0.0.1:8080/health2
This is the base shape used by the rest of the examples.
Example: JSON API
#include <vix.hpp>
#include <vix/middleware.hpp>
using namespace vix;
int main()
{
App app;
app.use("/api/users", middleware::app::json_strict_dev(4096));
app.post("/api/users", [](Request &req, Response &res)
{
auto &body = req.state<middleware::parsers::JsonBody>();
const std::string name = body.value.value("name", "");
if (name.empty())
{
res.status(422).json({
"ok", false,
"error", "Missing name"
});
return;
}
res.status(201).json({
"ok", true,
"name", name
});
});
app.run(8080);
}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
Test:
curl -i \
-X POST http://127.0.0.1:8080/api/users \
-H "Content-Type: application/json" \
-d '{"name":"Ada"}'2
3
4
This introduces the main middleware pattern:
middleware parses reusable input
handler reads typed request state
handler focuses on application logic2
3
Example: middleware stack
#include <vix.hpp>
#include <vix/middleware.hpp>
using namespace vix;
int main()
{
App app;
app.use("/api", middleware::app::security_headers_dev());
app.use("/api", middleware::app::cors_dev({"https://example.com"}));
app.use("/api", middleware::app::rate_limit_dev());
app.use("/api", middleware::app::body_limit_write_dev(1024 * 1024));
app.use("/api/users", middleware::app::json_strict_dev(4096));
app.get("/api/health", [](Request &, Response &res)
{
res.json({
"ok", true
});
});
app.post("/api/users", [](Request &req, Response &res)
{
auto &body = req.state<middleware::parsers::JsonBody>();
res.status(201).json({
"ok", true,
"body", body.value.dump()
});
});
app.run(8080);
}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
This is the kind of backend shape Vix middleware is designed for:
security headers
CORS
rate limit
body limit
strict JSON parser
route handler2
3
4
5
6
Static files belong to App
Static file serving is a Core App feature.
Use:
app.static_dir(
"public",
"/",
"index.html",
true,
"public, max-age=3600",
true,
false
);2
3
4
5
6
7
8
9
Do not treat static files as middleware.
Middleware can enhance static responses, for example with a compression hook, but Core serves the files.
The mental model is:
vix::App serves static files
vix::middleware enhances HTTP behavior
bootstrap code wires configuration2
3
HTTP cache is for dynamic GET routes
The HTTP cache example is about dynamic route responses.
app.use("/api", middleware::app::http_cache({
.ttl_ms = 30'000,
.allow_bypass = true,
.bypass_header = "x-vix-cache",
.bypass_value = "bypass"
}));2
3
4
5
6
This is different from static file Cache-Control.
app.static_dir(...)
controls public file serving and static cache headers
middleware::app::http_cache(...)
stores and replays dynamic GET route responses2
3
4
5
Production-style examples
The Production Bootstrap example shows a more realistic structure.
It is useful when your app needs:
.env configuration
server settings
public path
static file configuration
optional static compression
middleware registry
route registry
AppBootstrap class2
3
4
5
6
7
8
This is closer to generated projects and production backends.
The goal is to keep main() small and move startup wiring into a bootstrap layer.
Add new examples carefully
Before adding a new example, check that it is:
based on the current Vix API
small enough to understand quickly
runnable with vix run or clearly marked otherwise
tested with curl or a simple command
focused on one clear idea
not duplicating another example2
3
4
5
6
Avoid examples that depend on uncertain APIs.
Good example names are specific:
hello-app
json-api
middleware-api
auth-api-key
static-site
http-cache
production-bootstrap2
3
4
5
6
7
Avoid vague names:
auth
cache
database
middleware
production-app2
3
4
5
Specific names make the docs easier to navigate.
Summary
Use these examples to learn Vix by building real pieces:
start an app
return JSON
parse request bodies
protect routes
serve files
cache GET responses
structure production startup2
3
4
5
6
7
The examples should stay modern, small, and aligned with the current vix::App API.
If an old example no longer matches the current API, remove it or rewrite it.
Correct examples build trust.