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

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

example/example.dart

// doctag<hello-world-app>
import 'package:relic/io_adapter.dart';
import 'package:relic/relic.dart';

/// A simple 'Hello World' server demonstrating basic Relic usage.
Future<void> main() async {
  // Setup the app.
  final app = RelicApp()
    // Route with parameters (:name & :age).
    ..get('/user/:name/age/:age', helloHandler)
    // Middleware on all paths below '/'.
    ..use('/', logRequests())
    // Custom fallback - optional (default is 404 Not Found).
    ..fallback = respondWith(
      (_) => Response.notFound(
        body: Body.fromString("Sorry, that doesn't compute.\n"),
      ),
    );

  // Start the server (defaults to using port 8080).
  await app.serve();
}

/// Handles requests to the hello endpoint with path parameters.
Response helloHandler(final Request req) {
  final name = req.rawPathParameters[#name];
  final age = int.parse(req.rawPathParameters[#age]!);

  return Response.ok(
    body: Body.fromString('Hello, $name! To think you are $age years old.\n'),
  );
}

// end:doctag<hello-world-app>
68
likes
160
points
52.4k
downloads

Publisher

verified publisherserverpod.dev

Weekly Downloads

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

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#server #web #backend #http

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