http_server library Null safety

Utilities for working with HttpServer.

Example

Serving all files from the current directory.

import 'dart:io';

import 'package:http_server/http_server.dart';

Future<void> serveCurrentDirectory() async {
  var staticFiles = VirtualDirectory('.')..allowDirectoryListing = true;

  var server = await HttpServer.bind('0.0.0.0', 7777);
  print('Server running');
  server.listen(staticFiles.serveRequest);
}

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 wrapper around a file upload.
HttpBodyHandler
A handler for processing and collecting HTTP message data in to an HttpBody. [...]
HttpClientResponseBody
The body of a HttpClientResponse. [...]
HttpMultipartFormData
The data in a multipart/form-data part. [...]
HttpRequestBody
The body of a HttpRequest. [...]
VirtualDirectory
A VirtualDirectory can serve files and directory-listing from a root path, to HttpRequests. [...]
VirtualHost
A utility for handling multiple hosts on multiple sources using a named-based approach.