website/docs/getting-started.md

1.0 KiB

Getting Started

The easiest way to get started is to use the starter dependency, which automatically includes the components needed to boot up a simple HTTP server.

In the example below, we create a new D project named my-server, and add the handy-http-starter dependency to it.

dub init my-server
cd my-server
dub add handy-http-starter

Then, edit your main D function like so:

import handy_http_starter;

void main() {
    startServer((ref request, ref response) {
        response.headers.add("Content-Type", "text/plain");
        response.headers.add("Content-Length", "12");
        response.outputStream.write(cast(ubyte[]) "Hello world!")
    });
}

In the above code, we take advantage of Handy-Http Starter's shortcuts to quickly boot up an HTTP server on http://localhost:8080 that responds to any request with Hello world!, by executing the provided delegate function whenever a request is received.