relic 0.8.0 copy "relic: ^0.8.0" to clipboard
relic: ^0.8.0 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((final _) => 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 NewContext 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.'),
    ),
  );
}
46
likes
140
points
19.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
Contributing

Documentation

API reference

License

BSD-3-Clause (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