-
Notifications
You must be signed in to change notification settings - Fork 35
Serving static files
Serving static files is a common task in server-side programming. A good server-side framework should provide an easy and intuitive way to serve static files. Jaguar does this elegantly out-of-the-box through staticFiles
method.
Lets look at a sample code before we delve into details:
main(List<String> arguments) async {
final server = new Jaguar();
// Serve with prefix stripped. Similar to 'alias' in Nginx.
server.staticFiles('/static/*', 'static'); // The magic!
await server.serve();
}
That one line is all you need to serve content under the directory static
with request path /static/*
.
The second argument directory
specifies the disk or virtual directory from which static file contents are retrieved.
The first argument path
is the HTTP request path that must match for the static content to be served. This follows the same rules as any other route path.
The named argument stripPrefix
determines if the matched portion of the request shall be discarded while looking up the file in directory
or not.
For example,
With path
/static/*
, the request path to directory path mapping is as follows:
-
/static/index.html
->directory.path
+index.html
-
/static/one/index.html
->directory.path
+one/index.html
-
static_files
Contains a full working example showcasing different ways in which static files can be served with Jaguar.
In the next article, learn how to write JSON REST API calls using built-in Jaguar serialization features.
Basics
- Route handler
- Path matching
- Path parameters
- Query parameters
- Serving static files
- Cookies
- Controller
- Parameter binding
- Hot reload
Serialization
Forms
Sessions
Authentication
- Basic authentication
- Form authentication
- JSON authentication
- Authorization
- OAuth
- MongoDb
- PostgreSQL
- MySQL
- Establish connection
- ORM
- Server sent events (SSE)
- Websockets
- systemd
- Docker
- AppEngine