dia 0.0.5 copy "dia: ^0.0.5" to clipboard
dia: ^0.0.5 copied to clipboard

outdated

A simple dart http server in Koa2 style. WARNING do not use it in production! It is alfa version and they need in more work!

example/dia_example.dart

import 'dart:io';

import 'package:dia/dia.dart' as dia;

/// Custom Context example
class MyContext extends dia.Context {
  String? additionalField;

  MyContext(HttpRequest request) : super(request);
}

/// Middleware example
dia.Middleware logger() => (ctx, next) async {
      final start = DateTime.now();
      await next();
      final diff = DateTime.now().difference(start).inMicroseconds;
      print('microseconds=$diff');
    };

void main() {
  final app = dia.App<MyContext>();

  /// Add logger middleware
  app.use(logger());

  /// Add additionalField value
  app.use((ctx, next) async {
    ctx.additionalField = 'additional value';
    await next();
  });

  /// Uncomment this to see throw example
  // app.use((ctx, next)async{
  //   ctx.throwError(401);
  // });

  /// final middleware to response
  app.use((ctx, next) async {
    ctx.contentType = ContentType.text;
    ctx.body = ctx.additionalField;
  });

  /// Start server listen on localhsot:8080
  app
      .listen('localhost', 8080)
      .then((info) => print('Server started on http://localhost:8080'));
}
22
likes
0
pub points
69%
popularity

Publisher

verified publisherawcoding.com

A simple dart http server in Koa2 style. WARNING do not use it in production! It is alfa version and they need in more work!

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on dia