Getting Started
Getting Started
In this first guide, we will:
Download the
angel_framework
package from Pub.Launch an
AngelHttp
server.Add some basic routes to an app.
Add a 404 error handler.
The source code for this example can be found here:
https://github.com/angel-dart/examples-v2/tree/master/docs_examples/getting_started
First Steps
This tutorial relies on the terminal/command-line, so if are not well-versed in using such tools, you should copy/paste the snippets found on this page.
If you have not yet installed the Dart SDK, then it is required that you do so before continuing:
https://www.dartlang.org/tools/sdk#install
In addition, the curl
tool will be used to send requests to our server:
https://curl.haxx.se/download.html
Also, you will need to have the Dart SDK in your PATH
environment variable, so that the dart
and pub
executables can be found from your command line:
https://www.java.com/en/download/help/path.xml
Finally, note that some steps will mention Unix-specific programs, like nano
. Windows users should instead use Notepad. Alternative programs will be mentioned where relevant.
Project Setup
The first thing we'll need to do is create a new directory (folder) for our project.
Next, we create a pubspec.yaml
file, and enter the following contents:
Now, just run pub get
, which will install the angel_framework
library, and its dependencies:
Launching an HTTP Server
Angel can speak different protocols, but more often than not, we'll want it to speak HTTP.
Create a directory named bin
, and a file within bin
named main.dart
.
Your folder structure should now look like this:
Add the following to bin/main.dart
:
Next, in your terminal, run the command dart bin/main.dart
. Your server will now be running, and will listen for input until you kill it by entering Control-C
(the SIGINT
signal) into the terminal.
Open a new terminal window, and type the following:
You'll just see a blank line, but the fact that you didn't see an error means that the server is indeed listening at port 3000
.
Adding a Route
By adding routes to our server, we can respond to requests sent to different URL's.
Let's a handler at the root of our server, and print a simple Hello, world!
message.
From this point, all new code needs to be added before the call to http.startServer
(or else it will never run).
Add this code to your program:
bin/main.dart
should now look like the following:
(Note that this is the last time the entire file will be pasted, for the sake of brevity.)
Now, if you rerun curl localhost:3000 && echo
, you'll see the message Hello, world!
printed to your terminal!
Route Handlers
Let's break down the line we just added:
It consists of the following components:
A call to
app.get
A string,
'/'
,A closure, taking two parameters:
req
andres
The call
res.write('Hello, world!')
, which isalso the return value of the aforementioned closure.
Angel.get
is one of several methods (addRoute
, post
, patch
, delete
, head
, get
) that can be used to add routes that correspond to HTTP methods to an Angel
server instance.
Combined with the path, '/'
, this signifies that whenever a request is sent to the root of our server, which in this case is the URL http://localhost:3000
, the attached closure should be invoked.
The path is important because it defines the conditions under which code should run. For example, if we were to visit http://localhost:3000/foo
, we'd just see a blank line printed again, because there is no route mounted corresponding to the path '/foo'
.
The two parameters, req
and res
, hold the types RequestContext
and ResponseContext
, respectively. We'll briefly cover these in the next section.
Finally, we call res.write
, which, as you may have surmised, prints a value to the outgoing HTTP response. That's how we are able to print Hello, world!
.
Printing Headers
Just as their names suggest, the RequestContext
and ResponseContext
classes are abstractions used to read and write data on the Web.
By reading the property req.headers
, we can access the HTTP headers sent to us by the client:
Run the following:
And you'll see output like the following:
Reading Request Bodies
Web applications very often have users send data upstream, where it is then handled by the server.
Angel has built-in functionality for parsing bodies of three MIME types:
application/json
application/x-www-form-urlencoded
multipart/form-data
(You can also handle others, but that's beyond the scope of this demo.)
So, as long as the user sends data in one of the above forms, we can handle it in the same way.
Add the following route. It will listen on the path '/greet'
for a POST
request, and then attempt to parse the incoming request body.
Afterwards, it reads the name
value from the body, and computes a greeting string.
To visit this, enter the following curl
command:
You should see Hello, Bob!
appear in your terminal.
Adding an Error Handler
In the previous example, you might have noticed this line:
Angel handles errors thrown while calling route handlers, preventing your server from crashing. Ultimately, all errors are wrapped in the AngelHttpException
class, or sent as-is if they are already instances of AngelHttpException
.
By default, either an HTML page is printed, or a JSON message is displayed (depending on the client's Accept
header). In many cases, however, you might want to do something else, i.e. rendering an error page, or logging errors through a service like Sentry.
To add your own logic, set the errorHandler
of your Angel
instance. It is a function that accepts 3 parameters:
AngelHttpException
RequestContext
ResponseContext
Note that we kept a reference to the previous error handler, so that existing logic can be reused if the case we wrote for is not handled.
To trigger a 400 Bad Request
and see our error handler in action, run the following:
You will now see 'Oops! You forgot to include your name.'
printed to the console.
Conclusion
Congratulations on creating your first Angel server! Hopefully this is just one of many more to come.
The choice is now yours: either continue reading the other guides posted on this site, or tinker around and learn the ropes yourself.
You can find angel_*
packages on the Pub site, and read the documentation found in their respective README
files:
https://pub.dartlang.org/packages?q=dependency%3Aangel_framework
Don't forget that for discussion and support, you can either file a Github issue, or join the Gitter chat:
Last updated