CodeBlanks Logo Icon CodeBlanks

Summary - Ktor server

Combine routing, plugins and WebSocket to write an API.

Summary - Ktor server

This group covered Ktor server, Routing, Application, Plugin and WebSocket. These are the foundations of building an HTTP server in Kotlin.

You open an embeddedServer, configure with Application.module, declare routes with routing, add plugins with install, and open real time connections with webSocket. Together they form a modern API server. The next group moves to serialization.

Practice Summary: server skeleton

Define val server = embeddedServer(Netty, port = 8080) { routing { get("/") { } } }. Print println("Server ready").

Starter code

Main.kt
// Summary: Ktor server
// write here
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.routing.*

fun main() {
    
}

Sample solution

Main.kt
fun main() {
    val server = embeddedServer(Netty, port = 8080) {
        routing {
            get("/") {
            }
        }
    }
    println("Server ready")
}

Expected output

Server ready

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is used to add a plugin to the server?

  • use
  • with
  • add
  • install Correct answer

Explanation: answer: install. Plugins are added with install.

2 Match all Ktor server concepts

Term Meaning
embeddedServer Starts server
routing Defines routes
Application Central point
install Adds plugin
webSocket Persistent connection

All lessons in Advanced