Angel3as backend server.
vim. Windows users should instead use
Notepad++. Alternative programs will be mentioned where relevant.
pubspec.yamlfile, and enter the following contents:
dart pub get, which will install the
angel3_frameworklibrary, and its related dependencies:
Angel3can speak different protocols, but more often than not, we'll want it to speak HTTP.
bin, and a file within
dart bin/main.dart. Your server will now be running, and will listen for input until you kill it by entering
SIGINTsignal) into the terminal.
http.startServer(or else it will never run).
bin/main.dartshould now look like the following:
curl localhost:3000 && echo, you'll see the message
Hello, world!printed to your terminal!
res.write('Hello, world!'), which is also the return value of the aforementioned closure.
'/', 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
res, hold the types
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
ResponseContextclasses are abstractions used to read and write data on the Web.
Angel3has built-in functionality for parsing bodies of three MIME types:
POSTrequest, and then attempt to parse the incoming reques t body.
namevalue from the body, and computes a greeting string.
Hello, Bob!appear in your terminal.
AngelHttpExceptionclass, or sent as-is if they are already instances of
Acceptheader). 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.
Angelinstance. It is a function that accepts 3 parameters:
400 Bad Requestand see our error handler in action, run the following:
'Oops! You forgot to include your name.'printed to the console.