|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +description: Build a simple "Echo" application. |
| 4 | +--- |
| 5 | + |
| 6 | +# Echo 🔊 |
| 7 | + |
| 8 | +:::info |
| 9 | +**Difficulty**: 🟢 Beginner<br/> |
| 10 | +**Length**: 15 minutes |
| 11 | + |
| 12 | +Before getting started, [read the prerequisites](/docs/overview#prerequisites) to make sure your development environment is ready. |
| 13 | +::: |
| 14 | + |
| 15 | +## Overview |
| 16 | + |
| 17 | +In this tutorial, we're going to build an app that exposes a single endpoint and responds with a dynamic response which echoes the path back. |
| 18 | + |
| 19 | +When we're done, we should be able to make a `GET` request to the `/<message>` endpoint with any `message`: |
| 20 | + |
| 21 | +```bash |
| 22 | +# <message> is "ping" |
| 23 | +curl --request GET \ |
| 24 | + --url http://localhost:8080/ping |
| 25 | + |
| 26 | +# <message> is "pong" |
| 27 | +curl --request GET \ |
| 28 | + --url http://localhost:8080/pong |
| 29 | +``` |
| 30 | + |
| 31 | +And our server should respond with the following responses: |
| 32 | + |
| 33 | +``` |
| 34 | +HTTP/1.1 200 OK |
| 35 | +Connection: close |
| 36 | +Content-Length: 21 |
| 37 | +Content-Type: text/plain; charset=utf-8 |
| 38 | +
|
| 39 | +
|
| 40 | +ping |
| 41 | +
|
| 42 | +--- |
| 43 | +
|
| 44 | +HTTP/1.1 200 OK |
| 45 | +Connection: close |
| 46 | +Content-Length: 21 |
| 47 | +Content-Type: text/plain; charset=utf-8 |
| 48 | +
|
| 49 | +
|
| 50 | +pong |
| 51 | +``` |
| 52 | + |
| 53 | +## Creating a new app |
| 54 | + |
| 55 | +To create a new Dart Frog app, open your terminal, `cd` into the directory where you'd like to create the app, and run the following command: |
| 56 | + |
| 57 | +```bash |
| 58 | +dart_frog create echo |
| 59 | +``` |
| 60 | + |
| 61 | +You should see the following output: |
| 62 | + |
| 63 | +``` |
| 64 | +✓ Creating echo (0.1s) |
| 65 | +✓ Installing dependencies (1.7s) |
| 66 | +
|
| 67 | +Created echo at ./echo. |
| 68 | +
|
| 69 | +Get started by typing: |
| 70 | +
|
| 71 | +cd ./echo |
| 72 | +dart_frog dev |
| 73 | +``` |
| 74 | + |
| 75 | +## Running the development server |
| 76 | + |
| 77 | +You should now have a directory called `echo` -- `cd` into it: |
| 78 | + |
| 79 | +```bash |
| 80 | +cd echo |
| 81 | +``` |
| 82 | + |
| 83 | +Then, run the following command: |
| 84 | + |
| 85 | +```bash |
| 86 | +dart_frog dev |
| 87 | +``` |
| 88 | + |
| 89 | +This will start the development server on port `8080`: |
| 90 | + |
| 91 | +``` |
| 92 | +✓ Running on http://localhost:8080 (1.3s) |
| 93 | +The Dart VM service is listening on http://127.0.0.1:8181/YKEF_nbwOpM=/ |
| 94 | +The Dart DevTools debugger and profiler is available at: http://127.0.0.1:8181/YKEF_nbwOpM=/devtools/#/?uri=ws%3A%2F%2F127.0.0.1%3A8181%2FYKEF_nbwOpM%3D%2Fws |
| 95 | +[hotreload] Hot reload is enabled. |
| 96 | +``` |
| 97 | + |
| 98 | +Make sure it's working by opening [http://localhost:8080](http://localhost:8080) in your browser or via `cURL`: |
| 99 | + |
| 100 | +```bash |
| 101 | +curl --request GET \ |
| 102 | + --url http://localhost:8080 |
| 103 | +``` |
| 104 | + |
| 105 | +If everything succeeded, you should see `Welcome to Dart Frog!`. |
| 106 | + |
| 107 | +## Creating a dynamic route |
| 108 | + |
| 109 | +Now that we have a running application, let's start by deleting the root route since we won't need it for this example: |
| 110 | + |
| 111 | +```bash |
| 112 | +rm routes/index.dart |
| 113 | +``` |
| 114 | + |
| 115 | +Next, let's create `routes/[message].dart`: |
| 116 | + |
| 117 | +```bash |
| 118 | +touch routes/[message].dart |
| 119 | +``` |
| 120 | + |
| 121 | +:::note |
| 122 | +The square brackets `[...]` indicates that the path segment is dynamic and will match anything. Learn more about [dynamic routes](/docs/basics/routes#dynamic-routes-). |
| 123 | +::: |
| 124 | + |
| 125 | +Finally, let's define an `onRequest` method in the newly created route: |
| 126 | + |
| 127 | +```dart |
| 128 | +import 'package:dart_frog/dart_frog.dart'; |
| 129 | +
|
| 130 | +Response onRequest(RequestContext context, String message) { |
| 131 | + return Response(body: message); |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +:::note |
| 136 | +Since this route handler corresponds to a dynamic route, we will received the parameter as an argument in `onRequest`. |
| 137 | +::: |
| 138 | + |
| 139 | +Save the changes and hot reload should kick in ⚡️ |
| 140 | + |
| 141 | +```bash |
| 142 | +[hotreload] - Application reloaded. |
| 143 | +``` |
| 144 | + |
| 145 | +Make sure it's working by opening [http://localhost:8080/ping](http://localhost:8080/ping) in your browser or via `cURL`: |
| 146 | + |
| 147 | +```bash |
| 148 | +curl --request GET \ |
| 149 | + --url http://localhost:8080/ping |
| 150 | +``` |
| 151 | + |
| 152 | +If everything succeeded, you should see your message echoed back -- in this case, we should see `ping`. |
| 153 | + |
| 154 | +Let's try making a different request by visiting [http://localhost:8080/pong](http://localhost:8080/pong) in your browser or via `cURL`: |
| 155 | + |
| 156 | +```bash |
| 157 | +curl --request GET \ |
| 158 | + --url http://localhost:8080/pong |
| 159 | +``` |
| 160 | + |
| 161 | +This time you should see `pong`. |
| 162 | + |
| 163 | +🎉 Congrats, you've created an `echo` application using Dart Frog. View the [full source code](https://github.com/VeryGoodOpenSource/dart_frog/tree/main/examples/echo). |
0 commit comments