Skip to content

Serving static files

Ravi Teja Gudapati edited this page Jan 9, 2019 · 6 revisions

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.

Example

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:

  1. /static/index.html -> directory.path + index.html
  2. /static/one/index.html -> directory.path + one/index.html

Example project

  1. static_files
    Contains a full working example showcasing different ways in which static files can be served with Jaguar.

References

  1. Jaguar Dart: Serving static files

What's next?

In the next article, learn how to write JSON REST API calls using built-in Jaguar serialization features.

Basics

Serialization

Forms

Sessions

Authentication

  • Basic authentication
  • Form authentication
  • JSON authentication
  • Authorization
  • OAuth

Database

Security

Real time

  • Server sent events (SSE)
  • Websockets

Deployment

  • systemd
  • Docker
  • AppEngine

API Documentation

Clone this wiki locally