Defining routes with extension functions

Using Kotlin extension functions, we can make the definition and installation of our service's routes easier to read and maintain. To do this, perform the following steps:

  1. Start by creating a new package under the /src directory, and name that package routes:

  1. Now, we'll create a new file within this package named /src/routes/route_root.kt.
  2. Without route_root.kt, we will define an extension function on the Route type to define how our root endpoint route should be handled:
// route_root.ktfun Route.root() {    get("/") {        call.respondText("Hello World", ContentType.Text.Plain)    }}

You'll notice that we've ...

Get Mastering Kotlin now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.