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:
npm install kotlin --save
- Finally, for this example, you will create a small REST API using ExpressJS. Add the ExpressJS library with:
npm install express --save
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 to something easy to type.
Your Kotlin source code should be placed in the directory
Let’s create your first Kotlin file here.
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.
Hi! This is Miquel, the author of the post. I hope you like what you are reading!
If you are looking for a freelance Android developer, look no further!
Check: http://beltran.work/with-me and I’ll be happy to chat with you!
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.
Thanks for getting this far! I hope I have inspired you to give Node.js a try.