Routing

class Routing(val application: Application) : Route

A root routing node of an Application. You can learn more about routing in Ktor from Routing.

Parameters

application

is an instance of Application for this routing node.

Constructors

Link copied to clipboard
fun Routing(application: Application)

Types

Link copied to clipboard

An installation object of the Routing plugin.

Functions

Link copied to clipboard
Link copied to clipboard
open override fun afterIntercepted()
Link copied to clipboard

Creates a child node in this node with a given selector or returns an existing one with the same selector.

Link copied to clipboard
suspend fun execute(context: ApplicationCall, subject: Unit)
Link copied to clipboard

Installs a handler into this route which is called when the route is selected for a call.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
operator fun invoke(body: Route.() -> Unit)

Allows using a route instance for building additional routes.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun trace(block: (RoutingResolveTrace) -> Unit)

Registers a function used to trace route resolution. Might be useful if you need to understand why a route isn't executed. To learn more, see Tracing routes.

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

List of child routes for this node.

Link copied to clipboard
override val developmentMode: Boolean = false
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Pipeline for receiving content

Link copied to clipboard
Link copied to clipboard

Pipeline for sending content

Extensions

Link copied to clipboard
fun Route.accept(contentType: ContentType, build: Route.() -> Unit): Route

Builds a route to match requests with the HttpHeaders.Accept header matching the specified contentType.

Link copied to clipboard

Gets an Application for this Route by scanning the hierarchy to the root.

Link copied to clipboard
fun Route.contentType(contentType: ContentType, build: Route.() -> Unit): Route

Builds a route to match requests with the HttpHeaders.ContentType header matching the specified contentType.

Link copied to clipboard

Creates a routing entry for specified path.

Link copied to clipboard
fun Route.default(localPath: String)
fun Route.default(localPath: File)

Specifies localPath as a default file to serve when folder is requested

Link copied to clipboard
fun Route.defaultResource(resource: String, resourcePackage: String? = null)

Specifies resource as a default resources to serve when folder is requested

Link copied to clipboard

Builds a route to match DELETE requests with the specified path.

Builds a route to match DELETE requests.

Link copied to clipboard
fun Route.file(remotePath: String, localPath: String = remotePath)
fun Route.file(remotePath: String, localPath: File)

Sets up routing to serve localPath file as remotePath

Link copied to clipboard
fun Route.files(folder: String)
fun Route.files(folder: File)

Sets up routing to serve all files from folder

Link copied to clipboard
fun <F : Any> Route.findPluginInRoute(plugin: Plugin<*, *, F>): F?

Finds the plugin F in the current Route. If not found, search in the parent Route.

Link copied to clipboard

Builds a route to match GET requests with the specified path.

Builds a route to match GET requests.

Link copied to clipboard

Return list of endpoints with handlers under this route.

Link copied to clipboard

Builds a route to match HEAD requests with the specified path.

Builds a route to match HEAD requests.

Link copied to clipboard
fun Route.header(name: String, value: String, build: Route.() -> Unit): Route

Builds a route to match a header with the specified name and value.

Link copied to clipboard
fun Route.host(host: String, port: Int = 0, build: Route.() -> Unit): Route

Creates a route to match a request's host and port. There are no any host resolutions/transformations applied to a host: a request host is treated as a string.

fun Route.host(hostPattern: Regex, port: Int = 0, build: Route.() -> Unit): Route
fun Route.host(hosts: List<String>, ports: List<Int> = emptyList(), build: Route.() -> Unit): Route

Creates a route to match a request host and port. There are no any host resolutions/transformations applied to a host: a request host is treated as a string.

fun Route.host(hosts: List<String>, hostPatterns: List<Regex>, ports: List<Int> = emptyList(), build: Route.() -> Unit): Route

Creates a route to match s request host and port. There are no any host resolutions/transformations applied to a host: a request host is treated as a string.

Link copied to clipboard
fun Route.localPort(port: Int, build: Route.() -> Unit): Route

Creates a route to match a port on which a call was received.

Link copied to clipboard
fun Route.method(method: HttpMethod, body: Route.() -> Unit): Route

Builds a route to match the specified HTTP method.

Link copied to clipboard
fun Route.optionalParam(name: String, build: Route.() -> Unit): Route

Builds a route to capture an optional parameter with specified name, if it exists.

Link copied to clipboard

Builds a route to match OPTIONS requests with the specified path.

Builds a route to match OPTIONS requests.

Link copied to clipboard
fun Route.param(name: String, value: String, build: Route.() -> Unit): Route

Builds a route to match a parameter with the specified name and value.

fun Route.param(name: String, build: Route.() -> Unit): Route

Builds a route to match a parameter with the specified name and captures its value.

Link copied to clipboard

Builds a route to match PATCH requests with the specified path.

Builds a route to match PATCH requests.

@JvmName(name = "patchTyped")
inline fun <R : Any> Route.patch(crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit): Route

Builds a route to match PATCH requests receiving a request body as content of the R type.

@JvmName(name = "patchTypedPath")
inline fun <R : Any> Route.patch(path: String, crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit): Route

Builds a route to match PATCH requests with the specified path receiving a request body as content of the R type.

Link copied to clipboard
fun Route.port(vararg ports: Int, build: Route.() -> Unit): Route

Creates a route to match a request port.

Link copied to clipboard

Builds a route to match POST requests with the specified path.

@JvmName(name = "postTyped")
inline fun <R : Any> Route.post(crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit): Route

Builds a route to match POST requests receiving a request body as content of the R type.

@JvmName(name = "postTypedPath")
inline fun <R : Any> Route.post(path: String, crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit): Route

Builds a route to match POST requests with the specified path receiving a request body as content of the R type.

Builds a route to match POST requests.

Link copied to clipboard
fun Route.preCompressed(vararg types: CompressedFileType = CompressedFileType.values(), configure: Route.() -> Unit)

Support pre-compressed files in the file system only (not just any classpath resource)

Link copied to clipboard

Builds a route to match PUT requests with the specified path.

Builds a route to match PUT requests.

@JvmName(name = "putTyped")
inline fun <R : Any> Route.put(crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit): Route

Builds a route to match PUT requests receiving a request body as content of the R type.

@JvmName(name = "putTypedPath")
inline fun <R : Any> Route.put(path: String, crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit): Route

Builds a route to match PUT requests with the specified path receiving a request body as content of the R type.

Link copied to clipboard
fun Route.resource(remotePath: String, resource: String = remotePath, resourcePackage: String? = null)

Sets up routing to serve resource as remotePath in resourcePackage

Link copied to clipboard
fun Route.resources(resourcePackage: String? = null)

Sets up routing to serve all resources in resourcePackage

Link copied to clipboard
fun Route.route(path: String, build: Route.() -> Unit): Route

Builds a route to match the specified path.

fun Route.route(path: String, method: HttpMethod, build: Route.() -> Unit): Route

Builds a route to match the specified HTTP method and path.

Link copied to clipboard
fun Route.singlePageApplication(configBuilder: SPAConfig.() -> Unit = {})

Serves a single-page application. You can learn more from Serving single-page applications.

Link copied to clipboard
fun Route.static(configure: Route.() -> Unit): Route

Create a block for static content

fun Route.static(remotePath: String, configure: Route.() -> Unit): Route

Create a block for static content at specified remotePath

Link copied to clipboard

Base package for relative resources calculations for static content

Link copied to clipboard

Base folder for relative files calculations for static content