ktor-server-core / io.ktor.routing / Routing

Routing

class Routing : Route

Root routing node for an Application

Parameters

application - is an instance of Application for this routing

Types

Feature

companion object Feature : ApplicationFeature<Application, Routing, Routing>

Installable feature for Routing

Constructors

<init>

Routing(application: Application)

Root routing node for an Application

Properties

application

val application: Application

is an instance of Application for this routing

Inherited Properties

children

val children: List<Route>

List of child routes for this node

parent

val parent: Route?

is a parent node in the tree, or null for root node.

selector

val selector: RouteSelector

is an instance of RouteSelector for this node.

Functions

interceptor

suspend fun interceptor(context: PipelineContext<Unit, ApplicationCall>): Unit

trace

fun trace(block: (RoutingResolveTrace) -> Unit): Unit

Register a route resolution trace function. See https://ktor.io/servers/features/routing.html#tracing for details

Inherited Functions

afterIntercepted

open fun afterIntercepted(): Unit

createChild

fun createChild(selector: RouteSelector): Route

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

handle

fun handle(handler: <ERROR CLASS><Unit, ApplicationCall>): Unit

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

invoke

operator fun invoke(body: Route.() -> Unit): Unit

Allows using route instance for building additional routes

toString

open fun toString(): String

Companion Object Properties

RoutingCallFinished

val RoutingCallFinished: EventDefinition<RoutingApplicationCall>

Event definition for when a routing-based call processing finished

RoutingCallStarted

val RoutingCallStarted: EventDefinition<RoutingApplicationCall>

Event definition for when a routing-based call processing starts

key

val key: AttributeKey<Routing>

Unique key that identifies a feature

Companion Object Functions

install

fun install(pipeline: Application, configure: Routing.() -> Unit): Routing

Feature installation script

Extension Properties

application

val Route.application: Application

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

conversionService

val ApplicationCallPipeline.conversionService: ConversionService

Lookup for a conversion service. Returns the default one if the feature wasn't installed

staticBasePackage

var Route.staticBasePackage: String?

Base package for relative resources calculations for static content

staticRootFolder

var Route.staticRootFolder: File?

Base folder for relative files calculations for static content

Extension Functions

accept

fun Route.accept(contentType: ContentType, build: Route.() -> Unit): Route

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

contentType

fun Route.contentType(contentType: ContentType, build: Route.() -> Unit): Route

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

createRouteFromPath

fun Route.createRouteFromPath(path: String): Route

Create a routing entry for specified path

default

fun Route.default(localPath: String): Unit
fun Route.default(localPath: File): Unit

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

defaultResource

fun Route.defaultResource(resource: String, resourcePackage: String? = null): Unit

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

delete

fun Route.delete(path: String, body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match DELETE requests with specified path

fun Route.delete(body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match DELETE requests

file

fun Route.file(remotePath: String, localPath: String = remotePath): Unit
fun Route.file(remotePath: String, localPath: File): Unit

Sets up routing to serve localPath file as remotePath

files

fun Route.files(folder: String): Unit
fun Route.files(folder: File): Unit

Sets up routing to serve all files from folder

get

fun Route.get(path: String, body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match GET requests with specified path

fun Route.get(body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match GET requests

head

fun Route.head(path: String, body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match HEAD requests with specified path

fun Route.head(body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match HEAD requests

header

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

Builds a route to match header with specified name and value

host

fun Route.host(host: String, port: Int = 0, build: Route.() -> Unit): Route
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
fun Route.host(hosts: List<String>, hostPatterns: List<Regex>, ports: List<Int> = emptyList(), build: Route.() -> Unit): Route

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

localPort

fun Route.localPort(port: Int, build: Route.() -> Unit): Route

Create a route to match the port on which the call was received.

method

fun Route.method(method: HttpMethod, body: Route.() -> Unit): Route

Builds a route to match specified method

optionalParam

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

Builds a route to optionally capture parameter with specified name, if it exists

options

fun Route.options(path: String, body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match OPTIONS requests with specified path

fun Route.options(body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match OPTIONS requests

param

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

Builds a route to match parameter with specified name and value

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

Builds a route to match parameter with specified name and capture its value

patch

fun Route.patch(path: String, body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match PATCH requests with specified path

fun Route.patch(body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match PATCH requests

fun <R : Any> Route.patch(body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit): Route

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

fun <R : Any> Route.patch(path: String, body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit): Route

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

port

fun Route.port(vararg ports: Int, build: Route.() -> Unit): Route

Create a route to match request port.

post

fun Route.post(path: String, body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match POST requests with specified path

fun <R : Any> Route.post(body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit): Route

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

fun <R : Any> Route.post(path: String, body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit): Route

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

fun Route.post(body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match POST requests

preCompressed

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

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

put

fun Route.put(path: String, body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match PUT requests with specified path

fun Route.put(body: <ERROR CLASS><Unit, ApplicationCall>): Route

Builds a route to match PUT requests

fun <R : Any> Route.put(body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit): Route

Builds a route to match PUT requests with receiving request body content of type R

fun <R : Any> Route.put(path: String, body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit): Route

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

resource

fun Route.resource(remotePath: String, resource: String = remotePath, resourcePackage: String? = null): Unit

Sets up routing to serve resource as remotePath in resourcePackage

resources

fun Route.resources(resourcePackage: String? = null): Unit

Sets up routing to serve all resources in resourcePackage

route

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

Builds a route to match specified path

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

Builds a route to match specified method and path

static

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