mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-20 11:29:38 +00:00
Bring the READMEs and the docs pages (en and ja) in line with the current header: - Fix the client sample that does not compile, the Transfer-Encoding statement for default headers, the verifier callback comment, the thread pool spawn rule, the sanitize_filename description, and the inverted Accept-Encoding sentence. - Describe recent behaviour: TLS errors behind Read/Write failures, Host validation, Connection: close for a body without length, content_reader rules, refused CONNECT, WebSocketClient proxies, CPPHTTPLIB_INTERIM_RESPONSE_MAX_COUNT, the SSE event size limit, and the payload limit of the Stream API. - Complete the API references of WebSocket, WebSocketClient and the stream:: helpers, and the SSE reconnect rules. - Fix cookbook pages: keychain loading on every backend, received Close status, a multipart sample that trusted the client's file name, an echo sample that sent binary as text, SNI, pre-request handler scope, stop() with WebSocket handlers, and the thread pool notes. - Restore the mutex in the LLM app's Chapter 5 code.
63 lines
2.1 KiB
Markdown
63 lines
2.1 KiB
Markdown
---
|
|
title: "S04. Serve Static Files"
|
|
order: 23
|
|
status: "draft"
|
|
---
|
|
|
|
To serve static files like HTML, CSS, and images, use `set_mount_point()`. Just map a URL path to a local directory, and the whole directory becomes accessible.
|
|
|
|
## Basic usage
|
|
|
|
```cpp
|
|
httplib::Server svr;
|
|
if (!svr.set_mount_point("/", "./public")) {
|
|
// ./public doesn't exist
|
|
return 1;
|
|
}
|
|
svr.listen("0.0.0.0", 8080);
|
|
```
|
|
|
|
`set_mount_point()` returns `false` when the directory doesn't exist, so check the return value.
|
|
|
|
`./public/index.html` is now reachable at `http://localhost:8080/index.html`, and `./public/css/style.css` at `http://localhost:8080/css/style.css`. The directory layout maps directly to URLs.
|
|
|
|
A request for a directory serves its `index.html`, and one without the trailing slash (`/docs`) is redirected (301) to `/docs/`. A symlink that resolves outside the mounted directory gets a 403.
|
|
|
|
## Multiple mount points
|
|
|
|
You can register more than one mount point.
|
|
|
|
```cpp
|
|
svr.set_mount_point("/", "./public");
|
|
svr.set_mount_point("/assets", "./dist/assets");
|
|
svr.set_mount_point("/uploads", "./var/uploads");
|
|
```
|
|
|
|
You can even mount multiple directories at the same path — they're searched in registration order, and the first hit wins.
|
|
|
|
## Combine with API handlers
|
|
|
|
Static files and API handlers coexist happily. For GET and HEAD, the mount points are searched first; handlers registered with `Get()` and friends run only when no file is found.
|
|
|
|
```cpp
|
|
svr.Get("/api/users", [](const auto &req, auto &res) {
|
|
res.set_content("[]", "application/json");
|
|
});
|
|
|
|
svr.set_mount_point("/", "./public");
|
|
```
|
|
|
|
This gives you an SPA-friendly setup: `/api/*` hits the handlers, everything else is served from `./public/`.
|
|
|
|
## Add MIME types
|
|
|
|
cpp-httplib ships with a built-in extension-to-Content-Type map, but you can add your own.
|
|
|
|
```cpp
|
|
svr.set_file_extension_and_mimetype_mapping("wasm", "application/wasm");
|
|
```
|
|
|
|
> **Warning:** The static file server methods are **not thread-safe**. Don't call them after `listen()` — configure everything before starting the server.
|
|
|
|
> For download-style responses, see [S06. Return a file download response](../s06-download-response).
|