Skip to content

Embedded Frontend

In this example, youโ€™ll learn how to serve a static frontendโ€”such as a Single Page Application (SPA)โ€”directly from your Zuno backend. This is useful for:

  • Fullstack apps with a C++ backend and JS frontend
  • Admin panels or dashboards
  • Offline-capable UIs bundled with the server

embedded-frontend/
โ”œโ”€โ”€ CMakeLists.txt
โ”œโ”€โ”€ public/
โ”‚ โ”œโ”€โ”€ index.html
โ”‚ โ”œโ”€โ”€ app.js
โ”‚ โ””โ”€โ”€ styles.css
โ””โ”€โ”€ main.cpp

cmake_minimum_required(VERSION 3.16)
project(embedded_frontend)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(
zuno
GIT_REPOSITORY https://github.com/zuno-framework/zuno.git
GIT_TAG main
)
FetchContent_MakeAvailable(zuno)
add_executable(embedded-frontend main.cpp)
target_link_libraries(embedded-frontend PRIVATE zuno)

#include <zuno/zuno.hpp>
#include <fstream>
#include <sstream>
int main() {
zuno::App app;
// Serve static frontend files (JS, CSS, images)
app.use(staticFiles("public"));
// Fallback route for SPAs
app.get("/*", [](Request& req, Response& res) {
res.html(zuno::readFile("./public/index.html"));
});
app.listen(3000);
}

index.html
<!DOCTYPE html>
<html>
<head>
<title>Zuno Frontend</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1>Hello from Zuno Frontend</h1>
<script src="app.js"></script>
</body>
</html>

Terminal window
cmake -B build
cmake --build build
./build/embedded-frontend

Then visit http://localhost:3000 to see your frontend served by Zuno.



Zuno lets you bundle your backend and frontend into a single, elegant binaryโ€”no Node.js required.