{"id":9251,"date":"2022-03-11T21:41:36","date_gmt":"2022-03-11T21:41:36","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9251"},"modified":"2022-03-14T19:31:02","modified_gmt":"2022-03-14T19:31:02","slug":"dart-web-server","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/dart-web-server\/","title":{"rendered":"Dart web server"},"content":{"rendered":"<p>I know, when you think Dart you possibly think Flutter, i.e. mobile UI. However Dart is not limited to mobile. Let&#8217;s create a simple webserver using <a href=\"https:\/\/pub.dev\/packages\/shelf\" rel=\"noopener\" target=\"_blank\">shelf 1.2.0<\/a>.<\/p>\n<p>First off, create a folder for our code and in there create a file with the name <em>pubspec.yaml<\/em>. This is used to store our dependencies. In this file we have the following<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nname: server\r\n\r\nenvironment:\r\n  sdk: &quot;&gt;=2.12.0 &lt;3.0.0&quot;\r\n\r\ndependencies:\r\n  shelf: ^1.2.0\r\n  shelf_router: ^1.0.0\r\n  shelf_static: ^1.0.0\r\n<\/pre>\n<p>The &#8216;name&#8217; is the name of your app &#8211; I am not being very inspiring calling mine <em>server<\/em>. <\/p>\n<p>Now we&#8217;ll just copy and paste the example from Shelf. So create a file named server.dart (the name doesn&#8217;t matter, again, I was uninspiring in my choice).<\/p>\n<p>So <em>server.dart<\/em> should contain the following (I&#8217;ve changed from localhost to 0.0.0.0 to access the site from another computer as this code is hosted on a headless server).<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport 'package:shelf\/shelf.dart';\r\nimport 'package:shelf\/shelf_io.dart' as shelf_io;\r\n\r\nvoid main() async {\r\n  var handler =\r\n      const Pipeline().addMiddleware(logRequests()).addHandler(_echoRequest);\r\n\r\n  var server = await shelf_io.serve(handler, '0.0.0.0', 8080);\r\n\r\n  \/\/ Enable content compression\r\n  server.autoCompress = true;\r\n\r\n  print('Serving at http:\/\/${server.address.host}:${server.port}');\r\n}\r\n\r\nResponse _echoRequest(Request request) =&gt;\r\n    Response.ok('Request for &quot;${request.url}&quot;');\r\n<\/pre>\n<p>Next we&#8217;ll want to update\/get the packages, so run<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndart pub get\r\n<\/pre>\n<p>Finally let&#8217;s run the server using<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndart server.dart\r\n<\/pre>\n<p>If all went well a server is run up on port 8080 and accessible via your preferred web browser. This simple example can be accessed using the following (for example)<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nhttp:\/\/192.168.1.1:8080\/hello\r\n<\/pre>\n<p><em>hello<\/em> is just used to get a response from the server, which will look like this <em>Request for &#8220;hello&#8221;<\/em>.<\/p>\n<p><strong>Routing<\/strong><\/p>\n<p>The above gives us a baseline to work from. We&#8217;re going to want to route requests to alternate functions, currently requests simple go to the <em>_echoRequest<\/em> function.<\/p>\n<p>We&#8217;ve actually already added the <em>shelf_router<\/em> to our pubspec, so just import the package and add the Router. We&#8217;re going to removing the handler and the _echoRequest function, so our main function looks like this now<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport 'package:shelf_router\/shelf_router.dart';\r\nimport 'package:shelf\/shelf.dart';\r\nimport 'package:shelf\/shelf_io.dart' as shelf_io;\r\n\r\nvoid main() async {\r\n\r\n  var app = Router();\r\n\r\n  app.get('\/hello', (Request request) {\r\n    return Response.ok('hello-world');\r\n  });\r\n\r\n  app.get('\/user\/&lt;user&gt;', (Request request, String user) {\r\n    return Response.ok('hello $user');\r\n  });\r\n\r\n  var server = await shelf_io.serve(app, '0.0.0.0', 8080);\r\n\r\n  server.autoCompress = true;\r\n\r\n  print('Serving at http:\/\/${server.address.host}:${server.port}');\r\n}\r\n<\/pre>\n<p><em>Note: This code is basically a copy of the example on <a href=\"https:\/\/pub.dev\/packages\/shelf_router\" rel=\"noopener\" target=\"_blank\">shelf_router<\/a>.<\/em> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I know, when you think Dart you possibly think Flutter, i.e. mobile UI. However Dart is not limited to mobile. Let&#8217;s create a simple webserver using shelf 1.2.0. First off, create a folder for our code and in there create a file with the name pubspec.yaml. This is used to store our dependencies. In this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[309],"tags":[],"class_list":["post-9251","post","type-post","status-publish","format-standard","hentry","category-dart"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9251"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9251\/revisions"}],"predecessor-version":[{"id":9265,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9251\/revisions\/9265"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}