Ktor for Mobile Developers: Is it Easy to Learn Server Side Coding? Part-1


To create your first simple Ktor application we are going to use Ktor Framework. Ktor can be used to create a variety of server and client-side applications.

1. Lightweight:

Use what you need. Ktor allows you to transparently configure only the functionality your project requires. No magic involved!

2. Extensible:

Extend what you need. With a configurable pipeline, you can create the extensions you need and place them anywhere you want.

3. Multiplatform:

Run it where you need it. Built from the ground up with Kotlin Multiplatform technology, you can deploy Ktor applications anywhere.

4. Asynchronous:

Scales as you need it. Using Kotlin coroutines, Ktor is truly asynchronous and highly scalable. Use the power of non-blocking development without the callback nightmare.

1. Generate a Ktor project:

If you’re starting a new project with Ktor, you can get started quickly using start.ktor.io to generate and download your project template.

2. Plugin for IntelliJ IDEA:

If you’re using IntelliJ IDEA, you can use the Ktor plugin not only to generate new projects but also to get significantly more functionality.

  • Use the Marketplace tab to browse and install plugins from the JetBrains Plugin Repository or from a custom plugin repository.
  • Use the Installed tab to browse installed plugins, enable, disable, update, or remove them.
The project structure directory
dependencies {
implementation “io.ktor:ktor-server-core:$ktor_version”
implementation “io.ktor:ktor-server-netty:$ktor_version”
implementation “ch.qos.logback:logback-classic:$logback_version”
implementation “io.ktor:ktor-serialization:$ktor_version”
testImplementation “io.ktor:ktor-server-tests:$ktor_version”
}
To start the server click on run button.
2021–03–25 14:13:28.803 [main] INFO Application — Autoreload is disabled because the development mode is off.2021–03–25 14:13:29.156 [main] INFO Application — Responding at http://0.0.0.0:8080
This localhost page can’t be found
fun Application.module(testing: Boolean = false) {
routing {
get("/") {
call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
}
/*---------------------*/
post { }
put { }
delete { }
}

}

routing:

Routing is the core Ktor feature for handling incoming requests in a server application. When the client makes a request to a specific URL (for example, /hello), the routing mechanism allows us to define how we want this request to be served. For more details, you can check it here Ktor Routing.

get:

It is request type (GET)made by the client. You can use other request type also like POST,PUT,DELETE.

call:

The ApplicationCall provides access to two main properties ApplicationRequest and ApplicationResponse. As their names indicate, they correspond to the incoming request and outgoing response.

Comments

Popular posts from this blog

Ktor for Mobile Developers: Is it Easy to Learn Server Side Coding? Part-2

Android 12 developer preview - Features and APIs Overview