无法访问KTor网站 [英] KTor site not reachable

查看:81
本文介绍了无法访问KTor网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ktor制作一个简单的http服务器.但是,当我进入站点(127.0.0.1:8080或0.0.0.0:8080)时,它不存在.它不会打印,也不会响应.
但是,如果我使用NanoHttpd而不是ktor,则一切正常.我怎么了?

  import io.ktor.application.call导入io.ktor.http.ContentType导入io.ktor.response.respondText导入io.ktor.routing.get导入io.ktor.routing.routing导入io.ktor.server.engine.embeddedServer导入io.ktor.server.netty.Netty有趣的main(){val server = EmbeddedServer(Netty,port = 8080){路由{get("/"){println("TEST")call.respondText("Hello World!",ContentType.Text.Plain)}}}server.start(等待= true)} 

输出为:

  [main]信息ktor.application-未指定ktor.deployment.watch模式,自动重装未激活[main] INFO ktor.application-响应于http://0.0.0.0:8080 

解决方案

可能是以下情况之一:

  1. 应用程序代码
  2. 运行配置

我倾向于运行配置而不是应用程序代码有问题.

应用代码

即使我认为您的问题与运行配置有关,如果不是,我还是在这里提供示例代码.

当我使用

这是我在第4步中描述的内容,即添加类路径后可以选择我的主类:

运行符号应为Kotlin徽标:

我建议安装 IntelliJ Ktor插件,以引导您的项目.它使用Gradle并引导所有内容,因此当您运行 ./gradlew run 时,您可以正确运行它,并且无需手动构建配置步骤即可访问它.

I want to make a simple http server using ktor. However, when I enter the site (127.0.0.1:8080 or 0.0.0.0:8080), it just isn't there. It doesn't print and doesn't respond.
However if I use NanoHttpd instead of ktor, everything works fine. What is my issue?

import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.response.respondText
import io.ktor.routing.get
import io.ktor.routing.routing
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty


fun main() {
    val server = embeddedServer(Netty, port = 8080) {
        routing {
            get("/") {
                println("TEST")
                call.respondText("Hello World!", ContentType.Text.Plain)
            }
        }
    }
    server.start(wait = true)
}

The output is just:

[main] INFO ktor.application - No ktor.deployment.watch patterns specified, automatic reload is not active
[main] INFO ktor.application - Responding at http://0.0.0.0:8080

解决方案

It could be one of the following things:

  1. The application code
  2. The run configuration

I am leaning towards there being a problem with the run configuration rather than the application code.

Application code

Even though I think your issue is with the run configuration, in case that it isn't, I'm providing sample code here.

When I use the IntelliJ Ktor plugin, the Ktor app is bootstrapped as follows:

Application.kt

package com.example

import io.ktor.application.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*

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

@kotlin.jvm.JvmOverloads
@Suppress("unused") // Referenced in application.conf
fun Application.module(testing: Boolean = false) {
    routing {
        get("/") {
            call.respondText("Hello, world!", ContentType.Text.Plain)
        }
    }
}

application.conf

ktor {
    deployment {
        port = 8080
        port = ${?PORT}
    }
    application {
        modules = [ com.example.ApplicationKt.module ]
    }
}

I've provided an example Ktor code base here: https://gitlab.com/tinacious/ktor-example

Run configuration

It could be your run configuration in IntelliJ. It needs to be set up as a Kotlin script (and not an Application). When it's set up as an Application, I get the same error you do. Here is how to set up the run configuration I have, allowing me to run the server in IntelliJ:

  1. Add a configuration
  2. Choose Kotlin script from the Templates section. Do not choose Application.
  3. Near the bottom where it says "Use classpath of module" choose your application.main.
  4. Near the top where it says "Main class" you should be able to choose your main application. I found this only showed up after I chose the classpath of the module.

Here are the relevant sections in the configuration:

Here is what I describe in step 4, i.e. I can choose my main class after the class path is added:

The run symbol should be a Kotlin logo:

I would recommend installing the IntelliJ Ktor plugin for bootstrapping your project. It uses Gradle and bootstraps everything so when you run ./gradlew run, you are running it properly and can access it without the manual build configuration step.

这篇关于无法访问KTor网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆