Your first Node.js app with Kotlin- 4 mins
Originally published in my Medium blog.
Kotlin is a next-generation programming language by the great people of JetBrains, which is gaining popularity with the Android development community as a replacement for old good Java.
This guide is more aimed for Android developers who want to do a first step into Node.js development, and it is based on my own experiences building a Slack bot with it.
Code available here: https://github.com/miquelbeltran/kotlin-node.js
Your first step will be installing Node in your system. Node comes with a handy package manager called npm. Once you have installed Node.js follow these steps to configure your project.
On your empty project folder, create a Node project with:
Install the Kotlin dependency:
Finally, for this example, you will create a small REST API using ExpressJS. Add the ExpressJS library with:
Your Node.js project is now setup. Time to add the Kotlin part.
gradle.build file should look like this:
kotlinOptions is essential.
moduleKind must be set to
commonjs to work with Node, and I also recommend to change the outputFile destination too something easy to type.
Your Kotlin source code should be placed in the directory
Let’s create your first Kotlin file here.
external fun require(module:String):dynamic
In this code example, I load the ExpressJS library, I create a GET endpoint that returns “I am a beautiful butterfly” as response, and listens on port 3000.
First, you’ll have to compile your Kotlin code to JS with Gradle.
Now, start your Node server.
It works! You can go to http://localhost:3000 to check that your server is running.
If you are an independent Android developer, having the power to quickly create micro-services can help you a lot to enhance your applications.
As you saw by this guide, building a small service took minutes, with barely any boilerplate code and you can use the same IDE you are already familiar with.