Class HttpServer


  • public class HttpServer
    extends Object

    Provides a Nitric HTTP Handler server.

    The example below starts a HTTP server with a HTTP handler for the path "/".

    
     package com.example;
    
     import io.nitric.http.HttpHandler;
     import io.nitric.http.HttpRequest;
     import io.nitric.http.HttpResponse;
     import io.nitric.http.HttpServer;
    
     public class Handler implements HttpHandler {
    
         @Override
         public HttpResponse handle(HttpRequest request) {
             return HttpResponse.build("Hello World");
         }
    
         public static void main(String... args) {
             HttpServer().start(new Handler());
         }
     }
     
    See Also:
    HttpHandler
    • Constructor Detail

      • HttpServer

        public HttpServer()
    • Method Detail

      • hostname

        public HttpServer hostname​(String hostname)
        Set the server hostname. The default hostname is '127.0.0.1'. You can also change the hostname by setting a 'CHILD_ADDRESS' environment variable which will be used at startup to override the default.
        Parameters:
        hostname - the server hostname
        Returns:
        the HttpServer instance
      • port

        public HttpServer port​(int port)
        Set the server port. The default port is 8080.
        Parameters:
        port - the server port
        Returns:
        the HttpServer instance
      • register

        public HttpServer register​(String path,
                                   HttpHandler function)
        Register the function for the given path.
        Parameters:
        path - the function route path (required)
        function - the function to register (required)
        Returns:
        the HttpServer instance
      • start

        public HttpServer start​(HttpHandler function)
        Start the Http server with the given function configured for the path "/".
        Parameters:
        function - the function (required)
        Returns:
        a new started Http function server with the given function
      • start

        public void start()
        Start the function server.