simple_http_router 2.0.1-beta
simple_http_router: ^2.0.1-beta copied to clipboard

A very simple http router / multiplexer to handle HttpRequests and middleware

example/example.dart

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

const port = 8080;
const address = 'localhost';

void indexHandler(HttpRequest req, [Context ctx]) {
  req.response.writeln('Hello World');
  req.response.close();
}

void setCookieMiddleware(HttpRequest req, [Context con]) {
  req.response.cookies.add(Cookie('session', 'visited'));
}

void main(List<String> args) async {
  final server = await HttpServer.bind('localhost', 8080);
  final router = Router();

  router.register(Route('/index', HttpMethodHandler(get: indexHandler)));

  final indexMiddlewareHandler =
      MiddlewareHandler(indexHandler, before: const [setCookieMiddleware]);
  router.register(
      Route('/cookie', HttpMethodHandler(get: indexMiddlewareHandler)));

  await for (final request in server) {
    router.handle(request);
  }
}
0
likes
90
pub points
0%
popularity

A very simple http router / multiplexer to handle HttpRequests and middleware

Repository (GitLab)
View/report issues

Documentation

API reference

Uploader

marvinhannott@gmail.com

License

MIT (LICENSE)

Dependencies

quiver

More

Packages that depend on simple_http_router