Getting Started
Getting started with Spry.
Installation
Install Spry run this command:
dart pub add spry
Or, update your pubspec.yaml
file:
dependencies:
spry: <latest | ^{version}>
Quick Start
Creates a new file app.dart
(or main.dart
| server.dart
):
import 'dart:io';
import 'package:spry/spry.dart';
import 'package:spry/ws.dart';
import 'package:spry/io.dart';
main() async {
final app = createSpry();
app.get('/', ((event) => 'Hello, Spry!');
final handler = toIOHandler(app);
final server = await HttpServer.bind('127.0.0.1', 3000);
server.listen(handler);
print('🎉 Server listen on http://127.0.0.1:3000');
}
Now run the development server using dart run
:
dart run app.dart
And tadaa! We have a web server running locally.
What happened?
Okay, let's now break down our example:
We first created an [Spry application][/guide/app] using createSpry()
. app
is a tiny server capable of matching requests, generating response and handling lifecycle hooks (such as errors):
final app = createSpry();
Then we adds our first endpoint. in Spry, we define request handlers using a closure preceded by a FutureOr<T> Function(Event)
type:
app.get('/', (event) => ...);
What is beautiful in Spry is that all you have to do to make a response, is to simply return it! Responses can be simple string, JSON objects, Uint8List or streams.
return '⚡️ Tadaa!';
We then use Spry’s built-in dart:io
platform support to wrap the app instance into a handler that HttpServer
can use:
final handler = toIOHandler(app);
Finally, we create an HTTP server from dart:io
and listen for requests to pass to the Spry app:
final server = await HttpServer.bind('127.0.0.1', 3000);
server.listen(handler);