0
votes

I am trying to learn to use ktor and I'm trying to display the text "Hello Ktor" at the root path but all I keep getting is this site can't be reached. This is my code:

import io.ktor.application.*
import io.ktor.http.ContentType
import io.ktor.response.respondText
import io.ktor.routing.get
import io.ktor.routing.routing

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

private val userData = "{\"users\": [\"Timi\", \"Tomi\", \"Temi\"]}"

@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {

    routing {
        get("/") {
            call.respondText("Hello Ktor", ContentType.Text.Plain)
        }

    }
}

What am I doing wrong?

2
How are you testing your code? Are you visiting http://localhost? You should try http://localhost:8080.ordonezalex
Yes that was the issue. I was visiting 0.0.0.0:8080 instead of localhost:8080Timi1ehin

2 Answers

2
votes

Your code is fine. I think you just need to access it correctly from browser. Try

127.0.0.1:8080

or

localhost:8080

or simply follow nice tutorial from official website.

enter image description here

0
votes

Are you trying to run without a main?

Did you follow this guide? https://ktor.io/quickstart/quickstart/gradle.html#intellij-extract-out-configuration-data

If you want to start from main, use embeddedServer. Otherwise you have to set the mainClassName.