simple_http_router 2.0.0-beta simple_http_router: ^2.0.0-beta copied to clipboard
A very simple http router / multiplexer to handle HttpRequests and middleware
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);
}
}