Dart API Referencedart:ioHttpServer

HttpServer abstract class

HTTP server.

abstract class HttpServer {
  factory HttpServer() => new _HttpServer();

  /**
   * 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]);

  /**
   * 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);

  /**
   * 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));

  /**
   * 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));

  /**
   * Stop server listening.
   */
  void close();

  /**
   * 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;

  /**
   * Sets the error handler that is called when a connection error occurs.
   */
  void set onError(void callback(e));

  /**
   * Set the timeout, in seconds, for sessions of this HTTP server. Default
   * is 20 minutes.
   */
  int set sessionTimeout(int timeout);
}

Constructors

factory HttpServer() #

factory HttpServer() => new _HttpServer();

Properties

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));

void set onError(void callback(e)) #

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

void set onError(void callback(e));

final int 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;

int set sessionTimeout(int timeout) #

Set the timeout, in seconds, for sessions of this HTTP server. Default is 20 minutes.

int set sessionTimeout(int timeout);

Methods

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));

void close() #

Stop server listening.

void close();

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]);

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);