Skip to main content

Hello world

Once you have Dart installed, it only takes a few lines of code to set up your Relic server. These are the steps you need to take to get a simple Hello world server up and running.

Create a Dart package

First, you need to create a new Dart package for your Relic server.

dart create -t console-full hello_world

Add the Relic dependency

Next, add the relic package as a dependency to your pubspec.yaml file.

cd hello_world
dart pub add relic

Edit the main file

Edit the bin/hello_world.dart:

Hello world server
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'),
);
}

What this code does:

  1. Router and route: RelicApp() configures routing and registers a /user route.
  2. Middleware: use('/', logRequests()) logs each request for all paths under /.
  3. Server and fallback: app.serve() starts on port 8080; fallback handles unmatched routes and returns a 404 (not found).

The result is a server that responds with a personalized greeting when you send a GET request matching /user/:name/age/:age.

Running locally

Start your server with:

dart bin/hello_world.dart

Then, open your browser and visit http://localhost:8080/user/Nova/age/27, or use curl:

curl http://localhost:8080/user/Nova/age/37

You should see:

Hello Nova! To think you are 27 years old.

Congratulations! You just ran your first Relic server.

tip

Relic works with hot reload out of the box. ⚡️ To use it, start your server in Debug mode from your IDE (or use dart run --enable-vm-service bin/hello_world.dart). Ensure that Hot Reload On Save is enabled in your IDE settings (typically, set it to manual which will trigger hot reload when you save your file).