angel_framework
package from Pub.AngelHttp
server.curl
tool will be used to send requests to our server:PATH
environment variable, so that the dart
and pub
executables can be found from your command line:nano
. Windows users should instead use Notepad. Alternative programs will be mentioned where relevant.pubspec.yaml
file, and enter the following contents:pub get
, which will install the angel_framework
library, and its dependencies:bin
, and a file within bin
named main.dart
.bin/main.dart
: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.3000
.Hello, world!
message.http.startServer
(or else it will never run).bin/main.dart
should now look like the following:curl localhost:3000 && echo
, you'll see the message Hello, world!
printed to your terminal!app.get
'/'
,req
and res
res.write('Hello, world!')
, which isAngel.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.'/'
, 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.http://localhost:3000/foo
, we'd just see a blank line printed again, because there is no route mounted corresponding to the path '/foo'
.req
and res
, hold the types RequestContext
and ResponseContext
, respectively. We'll briefly cover these in the next section.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!
.RequestContext
and ResponseContext
classes are abstractions used to read and write data on the Web.application/json
application/x-www-form-urlencoded
multipart/form-data
'/greet'
for a POST
request, and then attempt to parse the incoming request body.name
value from the body, and computes a greeting string.curl
command:Hello, Bob!
appear in your terminal.AngelHttpException
class, or sent as-is if they are already instances of AngelHttpException
.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.errorHandler
of your Angel
instance. It is a function that accepts 3 parameters:AngelHttpException
RequestContext
ResponseContext
400 Bad Request
and see our error handler in action, run the following:'Oops! You forgot to include your name.'
printed to the console.angel_*
packages on the Pub site, and read the documentation found in their respective README
files: