简体   繁体   English

是否可以使用 Kotlin 构建完整的 Node.js Web 应用程序?

[英]Is it possible to build a full Node.js web application using Kotlin?

If I understand correctly, with the release of Kotlin 1.1 , we can set JavaScript as a compile target for full compilation to JavaScript of Kotlin projects.如果我理解正确,随着Kotlin 1.1 的发布,我们可以将 JavaScript 设置为编译目标,以完全编译为 Kotlin 项目的 JavaScript。 Is it possible (or feasible) to write an entire Node.js application, such as an express webserver, using only Kotlin code?仅使用 Kotlin 代码编写整个 Node.js 应用程序(例如快速网络服务器)是否可能(或可行)?

As this question suggests, we can import Node modules into Kotlin classes:正如这个问题所暗示的,我们可以将 Node 模块导入 Kotlin 类:

external fun require(module: String): dynamic

val express = require('express')

...which seems like I can create an application using: ...似乎我可以使用以下方法创建应用程序:

val app = express()

Is this the Kotlin way to set up an express application?这是Kotlin设置快速应用程序的方式吗? Or should I declare a class as described in the docs :或者我应该按照文档中的描述声明一个类:

@JsModule("express")
external class Express { ... }

What is the canonical way to set up a Kotlin project for Node.js application development?为 Node.js 应用程序开发设置 Kotlin 项目的规范方法是什么? Is Kotlin's JavaScript interoperability robust enough to continue down this path, or will it be more trouble than it's worth? Kotlin 的 JavaScript 互操作性是否足够健壮以继续沿着这条道路前进,还是会比它的价值更麻烦?

Technically speaking, yes, provided the claim by Kotlin that:从技术上讲,是的,提供 Kotlin 的声明:

You can use Kotlin to interact with server-side JavaScript such as node.js您可以使用 Kotlin 与服务器端 JavaScript 进行交互,例如 node.js

Is correct, and the transpilation of Kotlin -> JS is reliable enough to be able to predict what JS is coming out, then you could write a Node app in Kotlin, much as you can write them in TypeScript.是正确的,Kotlin -> JS 的转译足够可靠,可以预测出什么 JS,然后你可以用 Kotlin 编写一个 Node 应用程序,就像你可以用 TypeScript 编写它们一样。

I suspect, personally, that you'd find it difficult, buggy, and rather short on support, but it might make a good academic exercise...maybe.我个人怀疑,你会发现它很难,有问题,而且缺乏支持,但它可能是一个很好的学术练习......也许吧。

Yes, it's possible https://kotlinlang.org/docs/reference/js-project-setup.html是的,有可能https://kotlinlang.org/docs/reference/js-project-setup.html

But, NIO was the biggest reason to use NodeJS instead of any language to build a backend solution.但是,NIO 是使用 NodeJS 而不是任何语言来构建后端解决方案的最大原因。 Now, with reactive first class support you can have a stack like Kotlin + Spring Reactive + Coroutines + R2DBC and build a simple micro service or any full enterprise solution.现在,有了反应式一流的支持,您可以拥有像 Kotlin + Spring Reactive + Coroutines + R2DBC 这样的堆栈,并构建一个简单的微服务或任何完整的企业解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM