angel3_frameworkpackage from Pub.
curltool will be used to send requests to our server:
PATHenvironment variable, so that the
pubexecutables can be found from your command line:
nano. Windows users should instead use Notepad. Alternative programs will be mentioned where relevant.
pubspec.yamlfile, and enter the following contents:
pub get, which will install the
angel3_frameworklibrary, and its dependencies:
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.
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.
angel3_*packages on the Pub site, and read the documentation found in their respective