relic 0.9.2 copy "relic: ^0.9.2" to clipboard
relic: ^0.9.2 copied to clipboard

A lightweight and flexible web server inspired by Shelf for building APIs and backend services.

example/example.dart

import 'package:relic/io_adapter.dart';
import 'package:relic/relic.dart';

/// A simple 'Hello World' server
Future<void> main() async {
  // Setup app
  final app =
      RelicApp()
        ..get(
          '/user/:name/age/:age',
          hello,
        ) // route with parameters (:name & :age)
        ..use('/', logRequests()) // middleware on all paths below '/'
        // custom fallback - optional (default is 404 Not Found)
        ..fallback = respondWith(
          (_) => Response.notFound(
            body: Body.fromString("Sorry, that doesn't compute"),
          ),
        );

  // Start the server. Defaults to using port 8080 on loopback interface
  await app.serve();
}

ResponseContext hello(final RequestContext ctx) {
  final name = ctx.pathParameters[#name];
  final age = int.parse(ctx.pathParameters[#age]!);

  return ctx.respond(
    Response.ok(
      body: Body.fromString('Hello $name! To think you are $age years old.'),
    ),
  );
}
68
likes
0
points
53.4k
downloads

Publisher

verified publisherserverpod.dev

Weekly Downloads

A lightweight and flexible web server inspired by Shelf for building APIs and backend services.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

async, collection, convert, crypto, http_parser, meta, mime, path, stack_trace, stream_channel, vm_service, web_socket, web_socket_channel

More

Packages that depend on relic