Web
Run your Spry app in edge runtimes with Web API compatibility.
In order to run Spry app in web compatible edge runtimes supporting fetch
API with Request and Response, use WebPlatform
platform to convert Spry app into a fetch-like function.
Pre dependencies
Usage
First, create app entry:
dart
import 'package:spry/spry.dart';
final app = () {
final app = Spry();
app.use((event) => Response.text('Hello world!'));
return app
}();
Create web entry:
dart
import 'package:spry/web.dart';
import 'app.dart';
final handler = const WebPlatform().createHandler(app);
Local testing
You can test using any compatible JavaScript runtime by passing a Request object.
dart
import 'package:web/web.dart';
import 'web.dart';
void main() async {
final request = Request('http://localhost/'.toJS);
final response = await handler(request);
print(await response.text().toDart); // Hello world!
}
Compile to JavaScript file:
bash
dart compile js web_test.dart -o web_test.js
Run the web_test.js
:
bash
bun run web_test.js
bash
node ./web_test.js
html
<script src="web_test.js"></script>