http_server library

Here is a short example of how to serve all files from the current directory.

import 'dart:io';
import 'dart:async';
import 'package:http_server/http_server.dart';

void main() {
  var staticFiles = new VirtualDirectory('.')
    ..allowDirectoryListing = true;

  runZoned(() {
    HttpServer.bind('0.0.0.0', 7777).then((server) {
      print('Server running');
      server.listen(staticFiles.serveRequest);
    });
  },
  onError: (e, stackTrace) => print('Oh noes! $e $stackTrace'));
}

Virtual directory

The VirtualDirectory class makes it easy to serve static content from the file system. It supports:

  • Range-based requests.
  • If-Modified-Since based caching.
  • Automatic GZip-compression of content.
  • Following symlinks, either throughout the system or inside a jailed root.
  • Directory listing.

See VirtualDirectory for more information.

Virtual host

The VirtualHost class helps to serve multiple hosts on the same address, by using the Host field of the incoming requests. It also works with wildcards for sub-domains.

var virtualHost = new VirtualHost(server);
// Filter out on a specific host
var stream1 = virtualServer.addHost('static.myserver.com');
// Wildcard for any other sub-domains.
var stream2 = virtualServer.addHost('*.myserver.com');
// Requests not matching any hosts.
var stream3 = virtualServer.unhandled;

See VirtualHost for more information.

Classes

HttpBody
A HTTP content body produced by HttpBodyHandler for either HttpRequest or HttpClientResponse.
HttpBodyFileUpload
A HttpBodyFileUpload object wraps a file upload, presenting a way for extracting filename, contentType and the data of the uploaded file.
HttpBodyHandler
HttpBodyHandler is a helper class for processing and collecting HTTP message data in an easy-to-use HttpBody object. The content body is parsed, depending on the Content-Type header field. When the full body is read and parsed the body content is made available. The class can be used to process both server requests and client responses. [...]
HttpClientResponseBody
The HttpBody of a HttpClientResponse will be of type HttpClientResponseBody. It contains the HttpClientResponse object for access to the headers.
HttpMultipartFormData
HttpMultipartFormData class used for 'upgrading' a MimeMultipart by parsing it as a 'multipart/form-data' part. The following code shows how it can be used. [...]
HttpRequestBody
The HttpBody of a HttpRequest will be of type HttpRequestBody. It provides access to the request, for reading all request header information and responding to the client.
VirtualDirectory
A VirtualDirectory can serve files and directory-listing from a root path, to HttpRequests. [...]
VirtualHost
The VirtualHost class is a utility class for handling multiple hosts on multiple sources, by using a named-based approach.