spry 0.0.4 spry: ^0.0.4 copied to clipboard
Spry is an HTTP middleware framework for Dart to make web applications and APIs more enjoyable to write.
import 'dart:io';
import 'package:spry/spry.dart';
void main() async {
final Spry spry = Spry();
// logger
spry.use((Context context, MiddlewareNext 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, MiddlewareNext next) async {
final Stopwatch stopwatch = Stopwatch()..start();
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.send('Hello World!');
}
// Listen
await spry.listen(handler, port: 3000);
print('Server running at http://localhost:3000/');
}