Http Router

A simple http router with support for vhosts.

Tested with Dart 0.4.7

Example

handlers/index.dart

import 'dart:io';
import 'package:http_router/http_router.dart';

class IndexHandler extends HttpRouteHandler {

  dynamic handler(ServiceManager serviceManager, HttpRequest request) {
    request.response.write('Hello, world');
    request.response.close();
  }

}

handlers/missing.dart

import 'dart:io';
import 'package:http_router/http_router.dart';

class FallbackHandler extends HttpRouteHandler {

  dynamic handler(ServiceManager serviceManager, HttpRequest request) {
    request.response.write('Missing');
    request.response.close();
  }

}

main.dart

import 'dart:io';
import 'dart:async';

import 'package:http_router/http_router.dart';
import 'package:service_manager/service_manager.dart';

import './handlers/index.dart';
import './handlers/missing.dart';

main() {

  // bind the server to a host and port
  Future<HttpServer> server = HttpServer.bind("127.0.0.1", 3000);

  // whenever the server finishes binding
  server.then((HttpServer server) {

    // Initialize the service manager
    var serviceManager = new ServiceManager([]);

    // Initialize the router with specified vhosts and routes
    var httpRouter = new HttpRouter(serviceManager, {
      'r:.*:3000': {
        'GET': {
          '/': new IndexHandler()
        }
      }
    }, new FallbackHandler());

    // pipe http requests to the http router
    server.pipe(httpRouter);

  });

}

Libraries

http_router