19

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?

Cy Rossignol
  • 16,216
  • 4
  • 57
  • 83
  • 6
    Why would you *want* to do that? – Jared Smith Oct 16 '17 at 17:35
  • 2
    @JaredSmith I guess I'm partly asking the same question... *"should I want to do this?"* I know there are other options, but I'm particularly interested in exploiting Kotlin's type-safety while improving my skills in a relatively expressive and runtime-agnostic language that I can apply to other types of projects. – Cy Rossignol Oct 16 '17 at 17:39
  • 1
    I'm going to argue that Javascript/Typescript and Kotlin are not sufficiently different in expressive power to make a purely language-based argument for one over the other (although Kotlin definitely gets the edge in the library department courtesy of Java). So what you are really asking is "should I use V8 or the JVM". I don't feel qualified to answer that, but unless you have very good reasons stick with the one that brought you to the dance. Its *probably* not worth the trouble vs. just flat-out switching to JavaScript/TypeScript... or sticking with Kotlin/JVM. – Jared Smith Oct 16 '17 at 17:43
  • @JaredSmith Well, I'm more interested in the *possibility* of this approach rather than its *correctness* as defined by the availability of existing technologies in this area. Kotlin is a relatively new language/ecosystem, so it may become a valid alternative for node development in the future. I agree with what you're saying and appreciate the input--if I needed to pick a language for an enterprise project, I'd certainly choose an industry-standard set of tools. But Kotlin seems to support the goal of the question, at least to some degree, so I'm interested to see how far this approach works. – Cy Rossignol Oct 16 '17 at 18:18
  • 1
    I guess my question remains *why*? What does Kotlin by you over JavaScript in a node.js environment? Lots of languages can be varying degrees of easily compiled to JS: clojure, haskell, ocaml, C/C++. Indeed, one can write node modules in another language and expose them to node via FFI (David Herman at Mozilla had a blog post about doing so in Rust). It's certainly possible, and it may even be *easy*, I'm just not sure what benefit accrues. – Jared Smith Oct 16 '17 at 20:51
  • @JaredSmith Good points. What does Kotlin offer here? Benefits? Not much, perhaps, aside from *yet another* cross-stack language. There may also be something gained by supplementing existing node apps, or by relatively easy reuse of familiar JS libraries. I don't have an answer to these questions yet, which is part of the reason why I'm playing with these ideas. I appreciate the practicality you bring to this post--I must seem foolish for digging into a solved problem. I pose my question in the spirit of experimentation and learning with full expectation that I may seek something unreasonable. – Cy Rossignol Oct 16 '17 at 22:08
  • @JaredSmith And, in regard to learning, our discussion here has already given me some things to think about. Thanks for taking the time to respond. – Cy Rossignol Oct 16 '17 at 22:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156919/discussion-between-jared-smith-and-cy-rossignol). – Jared Smith Oct 17 '17 at 12:46
  • I don't understand why are you asking this question. You refer both to someone's question (and the author claims his approach to be inconvenient) and to *official* documentation. I guess the choice is obvious. – Alexey Andreev Nov 11 '17 at 10:09
  • Thanks for your input, @Alexey. You're right--I've managed to piece together some details of this puzzle, but the resources I've found don't focus specifically on Node at a *project* level. I'm looking for input from a Kotlin JS expert or someone with experience building a full Node app with Kotlin who can answer about the overall practicability of this approach and provide suggestions for a solid and reasonable foundation for Node projects written with Kotlin. If this type of project isn't possible (or sustainable), I'm interested to learn about the specific obstacles. – Cy Rossignol Nov 13 '17 at 01:01

2 Answers2

3

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.

Owen C. Jones
  • 1,435
  • 1
  • 11
  • 13
1

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.