Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Is it possible (or feasible) to write an entire Node.js application, such as an express webserver, using only Kotlin code?

As this question suggests, we can import Node modules into Kotlin classes:

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? 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? Is Kotlin's JavaScript interoperability robust enough to continue down this path, or will it be more trouble than it's worth?

like image 991
Cy Rossignol Avatar asked Oct 16 '17 17:10

Cy Rossignol


People also ask

Can I use node JS with Kotlin?

If Node. js is already installed on the host where you build Kotlin/JS projects, you can configure the Kotlin/JS Gradle plugin to use it instead of installing its own Node.

Is Kotlin good for web applications?

Can you build web applications with Kotlin? Although Kotlin is well-known among developers as a language for building Android apps and backends, it's also great for building web apps.

Can I use Kotlin for web development?

Kotlin is supported by the official integrated development environment (IDE) for Android development, Android Studio. So programmers can easily use Kotlin's code when working on Android development.

Can I use Kotlin for frontend?

Kotlin/JS frameworksYou can use both reactive and imperative programming models to build your frontend, use connectors for Ktor, Spring Boot, and other frameworks to integrate it with your server-side applications, and share code using Kotlin Multiplatform.


2 Answers

Technically speaking, yes, provided the claim by Kotlin that:

You can use Kotlin to interact with server-side JavaScript such as 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.

I suspect, personally, that you'd find it difficult, buggy, and rather short on support, but it might make a good academic exercise...maybe.

like image 147
Owen C. Jones Avatar answered Oct 10 '22 13:10

Owen C. Jones


Yes, it's possible 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. 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.

like image 38
Luiz Henrique Avatar answered Oct 10 '22 13:10

Luiz Henrique