Next Steps
You've learned the basics of Xpress++. Here's where to go from here.
Build a real API
Try building a complete REST API with all the pieces:
cpp
#include <xpresspp/xpresspp.h>
int main() {
xp::App app;
// ── Global middleware ───────────────────────────────
app.use(xp::requestId());
app.use(xp::logger());
app.use(xp::cors({.origin = "https://my-frontend.com"}));
app.use(xp::securityHeaders());
app.use(xp::rateLimit({.windowMs = 60000, .max = 200}));
// ── Public routes ───────────────────────────────────
app.get("/health", [](xp::Request& req, xp::Response& res) {
res.json({{"status", "ok"}});
});
// ── Protected routes ────────────────────────────────
app.group("/api/v1", [](xp::Router& r) {
r.get("/users", {xp::apiKeyAuth("secret")}, [](xp::Request& req, xp::Response& res) {
res.json({{"users", {}}});
});
r.post("/users", {xp::apiKeyAuth("secret"), xp::bodyLimit(50 * 1024)},
[](xp::Request& req, xp::Response& res) {
const auto body = req.json();
if (!body.isMember("name")) {
throw xp::BadRequestError("'name' is required.");
}
res.status(201).json({{"name", body["name"]}});
}
);
});
// ── Error handling ──────────────────────────────────
app.onError([](const std::exception& e, xp::Request& req, xp::Response& res) {
int status = 500;
if (const auto* he = dynamic_cast<const xp::HttpError*>(&e)) {
status = he->statusCode();
}
res.status(status).json({{"error", e.what()}});
});
// ── Start server ────────────────────────────────────
xp::AppConfig config;
config.port = xp::Config::envInt("PORT", 8080);
config.debug = xp::Config::envBool("DEBUG", true);
app.configure(config);
app.listen();
}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
39
40
41
42
43
44
45
46
47
48
49
50
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
39
40
41
42
43
44
45
46
47
48
49
50
Useful resources
- Drogon documentation — The HTTP engine underneath
- jsoncpp documentation — JSON API reference
- C++20 reference — Standard library
Contributing
Found a bug or want to add a feature? Pull requests are welcome!
- Fork the repo
- Create a branch:
git checkout -b feat/my-feature - Make your changes
- Test with
xp buildinsideexamples/simple - Open a PR
Getting help
- Open an issue on GitHub
- Check the sidebar for the relevant documentation page
- Run
xp doctorto diagnose dependency issues