/nanohttpd-champion

Tools to use NanoHTTPD like a champ.

Primary LanguageJavaMIT LicenseMIT

Travis build status AppVeyor build status Maven Central

nanohttpd-champion

Use NanoHTTPD like a champion.

Maven

<dependency>
    <groupId>com.github.mike10004</groupId>
    <artifactId>nanohttpd-server</artifactId>
    <version>0.14</version> <!-- use latest version identified by Maven badge above -->
</dependency>

Usage

import java.io.InputStream;
import java.nio.charset.Charset;
import io.github.mike10004.nanochamp.server.NanoControl;
import io.github.mike10004.nanochamp.server.NanoResponse;
import io.github.mike10004.nanochamp.server.NanoServer;

public class NanoServerExample {

    public static void main(String[] args) throws Exception {
        Charset charset = Charset.forName("UTF-8");
        NanoServer server = NanoServer.builder()
                .get(request -> NanoResponse.status(200).plainText("hello, world", charset))
                .build();
        try (NanoControl control = server.startServer();
            InputStream in = control.baseUri().toURL().openStream()) {
            byte[] buffer = new byte[1024];
            int r = in.read(buffer);
            String content = new String(buffer, 0, r, charset);
            System.out.println(content); // hello, world
        }
    }
}