【问题标题】:Ktor application not running on my computerKtor 应用程序未在我的计算机上运行
【发布时间】:2020-05-17 21:46:05
【问题描述】:

我正在尝试学习使用 ktor,并且正在尝试在根路径中显示文本“Hello Ktor”,但我一直得到的只是无法访问此站点。 这是我的代码:

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)
        }

    }
}

我做错了什么?

【问题讨论】:

  • 你如何测试你的代码?您在访问http://localhost 吗?你应该试试http://localhost:8080
  • 是的,这就是问题所在。我访问的是0.0.0.0:8080 而不是localhost:8080

标签: kotlin server ktor


【解决方案1】:

您的代码很好。 我认为您只需要从浏览器正确访问它即可。 试试

127.0.0.1:8080

本地主机:8080

或者简单地按照official website.的精彩教程进行操作

【讨论】:

    【解决方案2】:

    你想在没有 main 的情况下运行吗?

    您是否遵循本指南? https://ktor.io/quickstart/quickstart/gradle.html#intellij-extract-out-configuration-data

    如果您想从 main 开始,请使用 embeddedServer。否则你必须设置mainClassName

    【讨论】:

      猜你喜欢
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多