spry 2.0.2 copy "spry: ^2.0.2" to clipboard
spry: ^2.0.2 copied to clipboard

Spry is an HTTP middleware framework for Dart to make web applications and APIs server more enjoyable to write.

example/main.dart

import 'dart:io';

import 'package:spry/session.dart';
import 'package:spry/spry.dart';

void main() async {
  final Spry spry = Spry();

  // logger
  spry.use((Context context, Next next) async {
    await next();

    final Response response = context.response;
    final String? rt = response.headers.value('x-response-time');

    print('${context.request.method} ${context.request.uri} - $rt');
  });

  // x-response-time
  spry.use((Context context, Next next) async {
    final Stopwatch stopwatch = Stopwatch()..start();
    print(context.session['foo']);

    await next();

    stopwatch.stop();
    context.response.headers
        .set('x-response-time', '${stopwatch.elapsedMilliseconds}ms');
  });

  // Create handler
  handler(Context context) {
    context.response.statusCode = HttpStatus.ok;
    context.response.text('Hello World!');
    context.session['foo'] = 'bar';
    context.response.cookies.add(Cookie('a', 'b'));
    // context.response.cookies.add(value);
    // context.session.destroy();
  }

  // Listen
  final server = await spry.listen(handler, port: 3000);

  print('Server running at http://localhost:${server.port}/');
}
10
likes
0
pub points
54%
popularity

Publisher

verified publisherodroe.dev

Spry is an HTTP middleware framework for Dart to make web applications and APIs server more enjoyable to write.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

meta, mime

More

Packages that depend on spry