Embedded Frontend
๐ผ๏ธ Embedded Frontend
Section titled โ๐ผ๏ธ 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
๐งฑ Project Structure
Section titled โ๐งฑ Project Structureโembedded-frontend/โโโ CMakeLists.txtโโโ public/โ โโโ index.htmlโ โโโ app.jsโ โโโ styles.cssโโโ main.cpp
๐ CMakeLists.txt
Section titled โ๐ CMakeLists.txtโcmake_minimum_required(VERSION 3.16)project(embedded_frontend)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(zunoGIT_REPOSITORY https://github.com/zuno-framework/zuno.gitGIT_TAG main)
FetchContent_MakeAvailable(zuno)
add_executable(embedded-frontend main.cpp)target_link_libraries(embedded-frontend PRIVATE zuno)
๐ main.cpp
Section titled โ๐ main.cppโ#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 SPAsapp.get("/*", [](Request& req, Response& res) { res.html(zuno::readFile("./public/index.html"));});
app.listen(3000);}
๐ public/index.html
Section titled โ๐ public/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>
๐ Run the App
Section titled โ๐ Run the Appโcmake -B buildcmake --build build./build/embedded-frontend
Then visit http://localhost:3000 to see your frontend served by Zuno.
๐งญ Whatโs Next?
Section titled โ๐งญ Whatโs Next?โ- Add Brotli Compression for faster delivery
- Secure with TLS
- Combine with Authenticated Routes
Zuno lets you bundle your backend and frontend into a single, elegant binaryโno Node.js required.