Dart API Referencedart:ioHttpServer

HttpServer Interface

HTTP server.

Constructors

Code new HttpServer() #

HttpServer();

Methods

Code addRequestHandler(bool matcher(HttpRequest request), void handler(HttpRequest request, HttpResponse response)) #

Adds a request handler to the list of request handlers. The function matcher is called with the request and must return true if the handler should handle the request. The first handler for which matcher returns true will be handed the request.

addRequestHandler(bool matcher(HttpRequest request),
                  void handler(HttpRequest request, HttpResponse response));

Code void close() #

Stop server listening.

void close();

Code void set defaultRequestHandler(void handler(HttpRequest request, HttpResponse response)) #

Sets the default request handler. This request handler will be called if none of the request handlers registered by addRequestHandler matches the current request. If no default request handler is set the server will just respond with status code NOT_FOUND (404).

void set defaultRequestHandler(
    void handler(HttpRequest request, HttpResponse response));

Code void listen(String host, int port, [int backlog]) #

Start listening for HTTP requests on the specified host and port. If a port of 0 is specified the server will choose an ephemeral port. The optional argument backlog can be used to specify the listen backlog for the underlying OS listen setup. See addRequestHandler and defaultRequestHandler for information on how incoming HTTP requests are handled.

void listen(String host, int port, [int backlog]);

Code void listenOn(ServerSocket serverSocket) #

Attach the HTTP server to an existing ServerSocket. If the HttpServer is closed, the HttpServer will just detach itself, and not close serverSocket.

void listenOn(ServerSocket serverSocket);

Code void set onError(void callback(e)) #

Sets the error handler that is called when a connection error occurs.

void set onError(void callback(e));

Code int get port() #

Returns the port that the server is listening on. This can be used to get the actual port used when a value of 0 for port is specified in the listen call.

int get port();