---
title: "HttpServer"
sidebar_label: "HttpServer"
description: "Kotlin binding reference: HttpServer."
---

<!-- Generated by tools/api_reference/generate_api_docs.py. Do not edit. -->

//[clika-runtime](../../../index.md)/[io.clika.runtime](../index.md)/[HttpServer](index.md)

# HttpServer

[common]\
class [HttpServer](index.md)

The runtime's HTTP server: routes by verb and path (exact, or a pattern whose groups become captures), route groups, middleware, static files, the failure and error pages, an access logger, the tuning knobs, and the start and stop ([listen](listen.md) blocks; [listenAsync](listenAsync.md) / [bindToAnyPort](bindToAnyPort.md) serve on a background thread; [stop](stop.md) drains). A handler runs on the server's own thread; the runtime attaches it to the JVM for the call. `AutoCloseable`: [close](close.md) stops the server if it runs and releases it. A borrowed server ([isBorrowed](isBorrowed.md)) is another object's, handed out for its routes: [close](close.md) drops the handle and releases nothing, and the owner's start, stop and lifetime stay the owner's.

```kotlin
HttpServer.create().use { server ->
    server.get("/hello") { ServerResponse.text("hi") }
    val port = server.bindToAnyPort("127.0.0.1")
    println(Http.getText("http://127.0.0.1:$port/hello"))   // hi
    server.stop()
}
```

## Types

| Name | Summary |
|---|---|
| [Companion](Companion/index.md) | [common]<br>object [Companion](Companion/index.md) |

## Properties

| Name | Summary |
|---|---|
| [isBorrowed](isBorrowed.md) | [common]<br>val [isBorrowed](isBorrowed.md): [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) = false<br>True for a server another object owns; [close](close.md) then releases nothing. |
| [isRunning](isRunning.md) | [common]<br>val [isRunning](isRunning.md): [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)<br>True while the server serves. |
| [port](port.md) | [common]<br>val [port](port.md): [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)<br>The port served, once bound. |

## Functions

| Name | Summary |
|---|---|
| [addStaticFile](addStaticFile.md) | [common]<br>fun [addStaticFile](addStaticFile.md)(urlPath: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), file: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html))<br>Serve one [file](addStaticFile.md) at one exact [urlPath](addStaticFile.md). |
| [bindToAnyPort](bindToAnyPort.md) | [common]<br>fun [bindToAnyPort](bindToAnyPort.md)(host: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)): [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)<br>Serve on [host](bindToAnyPort.md) at a port the system picks, on a background thread; the port is answered. |
| [close](close.md) | [common]<br>open fun [close](close.md)() |
| [delete](delete.md) | [common]<br>fun [delete](delete.md)(path: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), handler: [Handler](../Handler/index.md)): [HttpServer](index.md)<br>A DELETE route. |
| [get](get.md) | [common]<br>fun [get](get.md)(path: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), handler: [Handler](../Handler/index.md)): [HttpServer](index.md)<br>A GET route (HEAD falls back to it when no HEAD route exists). |
| [group](group.md) | [common]<br>fun [group](group.md)(prefix: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)): [RouteGroup](../RouteGroup/index.md)<br>A group of routes under [prefix](group.md). |
| [listen](listen.md) | [common]<br>fun [listen](listen.md)(host: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), port: [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html))<br>Serve on [host](listen.md):[port](listen.md), blocking this thread until [stop](stop.md) is called from another. |
| [listenAsync](listenAsync.md) | [common]<br>fun [listenAsync](listenAsync.md)(host: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), port: [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html))<br>Serve on [host](listenAsync.md):[port](listenAsync.md) on a background thread; returns once the server accepts. |
| [mount](mount.md) | [common]<br>fun [mount](mount.md)(urlPrefix: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), dir: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html))<br>Serve the files under [dir](mount.md) at [urlPrefix](mount.md). |
| [patch](patch.md) | [common]<br>fun [patch](patch.md)(path: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), handler: [Handler](../Handler/index.md)): [HttpServer](index.md)<br>A PATCH route. |
| [post](post.md) | [common]<br>fun [post](post.md)(path: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), handler: [Handler](../Handler/index.md)): [HttpServer](index.md)<br>A POST route. |
| [put](put.md) | [common]<br>fun [put](put.md)(path: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), handler: [Handler](../Handler/index.md)): [HttpServer](index.md)<br>A PUT route. |
| [route](route.md) | [common]<br>fun [route](route.md)(method: [Method](../Method/index.md), path: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), handler: [Handler](../Handler/index.md)): [HttpServer](index.md)<br>Register [handler](route.md) for [method](route.md) at the exact [path](route.md). Returns this for chaining. |
| [routePattern](routePattern.md) | [common]<br>fun [routePattern](routePattern.md)(method: [Method](../Method/index.md), pattern: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), handler: [Handler](../Handler/index.md)): [HttpServer](index.md)<br>Register [handler](routePattern.md) for [method](routePattern.md) at a path [pattern](routePattern.md) (a regular expression; its groups are the captures). |
| [setBodyDeadline](setBodyDeadline.md) | [common]<br>fun [setBodyDeadline](setBodyDeadline.md)(seconds: [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html))<br>The ceiling from the end of the headers to the end of the declared body, in seconds (0 disables it). |
| [setDefaultHeader](setDefaultHeader.md) | [common]<br>fun [setDefaultHeader](setDefaultHeader.md)(name: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), value: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html))<br>A header appended to every response. |
| [setErrorHandler](setErrorHandler.md) | [common]<br>fun [setErrorHandler](setErrorHandler.md)(handler: [ErrorHandler](../ErrorHandler/index.md)?)<br>The page of an error status such as a 404 (null restores the default page). |
| [setExceptionHandler](setExceptionHandler.md) | [common]<br>fun [setExceptionHandler](setExceptionHandler.md)(handler: [ExceptionHandler](../ExceptionHandler/index.md)?)<br>The page of a handler that raised (null restores the default 500). |
| [setHeaderDeadline](setHeaderDeadline.md) | [common]<br>fun [setHeaderDeadline](setHeaderDeadline.md)(seconds: [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html))<br>The ceiling from a request's first byte to the end of its headers, in seconds (0 disables it). |
| [setKeepAliveTimeout](setKeepAliveTimeout.md) | [common]<br>fun [setKeepAliveTimeout](setKeepAliveTimeout.md)(seconds: [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html))<br>How long an idle connection stays open between requests, in seconds. |
| [setLogger](setLogger.md) | [common]<br>fun [setLogger](setLogger.md)(logger: [Logger](../Logger/index.md)?)<br>The per-request access logger (null removes it). |
| [setMaxConnections](setMaxConnections.md) | [common]<br>fun [setMaxConnections](setMaxConnections.md)(count: [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html))<br>The concurrent-connection ceiling; 0 means unlimited. |
| [setMimeType](setMimeType.md) | [common]<br>fun [setMimeType](setMimeType.md)(extension: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), mime: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html))<br>Map a file [extension](setMimeType.md) (with its dot) to a [mime](setMimeType.md) type for the static routes. |
| [setPayloadMaxLength](setPayloadMaxLength.md) | [common]<br>fun [setPayloadMaxLength](setPayloadMaxLength.md)(bytes: [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html))<br>The largest request body accepted, in bytes. |
| [setReadTimeout](setReadTimeout.md) | [common]<br>fun [setReadTimeout](setReadTimeout.md)(seconds: [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html))<br>How long one read waits for the next byte of a request, in seconds. |
| [setTls](setTls.md) | [common]<br>fun [setTls](setTls.md)(certPath: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), keyPath: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html))<br>Serve HTTPS with this certificate and private key (PEM file paths). |
| [setWriteTimeout](setWriteTimeout.md) | [common]<br>fun [setWriteTimeout](setWriteTimeout.md)(seconds: [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html))<br>The per-connection write timeout, in seconds. |
| [stop](stop.md) | [common]<br>fun [stop](stop.md)()<br>Stop accepting and drain: in-flight handlers finish and their responses flush. |
| [use](use.md) | [common]<br>fun [use](use.md)(middleware: [Middleware](../Middleware/index.md)): [HttpServer](index.md)<br>Add a middleware to every route (run in registration order). |
| [waitUntilReady](waitUntilReady.md) | [common]<br>fun [waitUntilReady](waitUntilReady.md)(timeoutMs: [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) = 30000)<br>Block until the server accepts, or fail after [timeoutMs](waitUntilReady.md). |