Summary - Ktor server
- Kotlin course
- Advanced
- Lesson 24/83
- 5 min
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
// 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
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 |