The class HttpServer
is used to write web servers; this server listens on (or binds to) a particular host and port for incoming HTTP requests. It provides event handlers (better called request handlers
in this case) that are triggered when a request with incoming data from a web client is received.
We make a project called simple_webserver
starting from the template command-line application and import dart:io
as follows:
import 'dart:io'; //Define host and port: InternetAddress HOST = InternetAddress.LOOPBACK_IP_V6; const int PORT = 8080; main() { // Starting the web server: HttpServer.bind(HOST, PORT) .then((server) { print('server starts listening on port ${server.port}'), // Starting the request handler: server.listen(handleRequest); }) .catchError(print); } handleRequest(HttpRequest req) { print('request coming in'), req.response ..headers.contentType = new ContentType("text", "plain", charset: "utf-8") ..write(' I heard you loud and clear.') ..write(' Send me the data!') ..close(); }
A web server runs on a host (either specified by a name or an IP address) and uses a port on that host to listen for requests. We define these here upfront as stated in comment 1. HOST could be a string, such as localhost, or an object of the class InternetAddress
. The LOOPBACK
schema is the same as localhost; this is used for testing on a local machine. For production purposes, use ANY_IP_V6
to allow for incoming connections from the network. Instead of IP_V6
, you could also use IP_V4
, but IP_V6
is more general and includes an IP_V4
listener.
A port can be any valid number above 1024 that is not in use. If another program is already listening on the same port (or the server is still running), an error occurs.
Next, we use the static method bind
to create the web server; this returns a Future object to run asynchronously. When the bind is successful, the callback then()
is called with the new HttpServer
object as a parameter. We print out the port to the console, so we can confirm it is running. The
catchError
function will be triggered in the case of an exception and equally prints to the console.
Next, the callback handler handleRequest
is set up; it will be triggered for any incoming request that it accepts as a parameter. In other words when a request comes in, the server creates an HttpRequest
object and passes it to the callback handleRequest
of listen()
.
In this first example, we write to its response
object after first setting the content type in the headers and close it when we're done. The response
object is of the class HttpResponse
; it will contains the server's answer to the request.
To see it in action, start the server from the editor or on the command line with the following command:
dart simple_webserver.dart
This produces the following server console output:
Then, start any browser with the URL http://localhost:8080
to see the response text appear on the client as shown in the following screenshot:
The HttpRequest
object also has properties that provide information about the client's request; the most important ones are as follows:
method
: This is derived from the way the web form was submitted. In the <form action="http://localhost:4041"method="GET">
code, the values it can take are GET
, POST
, PUT
, or DELETE
.headers
: This gives general information on the request, such as content type, content length, and date.uri
: This gives the location where the request originated from.