Search code examples
javascriptnode.jsexpresskotlinkotlin-interop

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?


Solution

  • 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.