From 8db0028cdaaf009df24592422172ae29d009f399 Mon Sep 17 00:00:00 2001 From: Rich Rose Date: Tue, 6 Dec 2022 21:52:44 +0000 Subject: [PATCH] Create ex13-6.md Add HTTP Server example --- ch13/ex13-6.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ch13/ex13-6.md diff --git a/ch13/ex13-6.md b/ch13/ex13-6.md new file mode 100644 index 0000000..81cd4cf --- /dev/null +++ b/ch13/ex13-6.md @@ -0,0 +1,28 @@ +# 13.6 Backend HTTP Server +## Example: HTTP Server +```dart +import 'dart:convert'; +import 'dart:io'; + +const port = 8080; + +Future main(List arguments) async { + final httpServer = await createHTTPServer(); + await httpRequest(httpServer); +} + +Future createHTTPServer() async { + final intAddress = InternetAddress.anyIPv4; + return await HttpServer.bind(intAddress, port); +} + +Future httpRequest(HttpServer server) async { + await for (HttpRequest request in server) { + if (request.method == 'GET'){ + request.response + ..write(' { "data": [ {"title": "January"}, {"title:": "February"}, {"title": "March"} ] }\n') + ..close(); + } + } +} +```