Kotlin + Node.js 搭建教程

Kotlin是JetBrains推出的一款语言, 相比Java有更简洁的语法, 能编译为Java Class, 也能编译为JavaScript
Node.js则是可以运行在服务端的JavaScript, 这里把二者结合, 搭建一个用Kotlin编写的服务端应用

创建

打开Idea 创建一个 Kotlin(JavaScript) 项目
编写一个测试文件, 检查是否可以正常编译

Test.kt

1
2
3
fun main(args: Array<String>) {
println("hello kt")
}

Ctrl+F9编译, 如果看到生成了编译文件, 就可以了, 其中{projectName}.js就是编译后的文件, 打开可以看到已经被编译为JavaScript了, 其中也有println('hello kt');
如果没问题的话就可以正式开始接下来的了, 创建App.kt

App.kt

监听8888端口 对任何请求都返回hello world

1
2
3
4
5
6
7
8
9
10
11
12
13
import kotlin.js.json

external fun require(module: String): dynamic

fun main(args: Array<String>) {
println("hello kt")
val http = require("http")

http.createServer { _, response ->
response.writeHead(200, json("Content-Type" to "text/plain"))
response.end("Hello World")
}.listen(8888)
}

NPM

打开终端运行

1
$ npm init

package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"name": "kt-node",
"version": "1.0.0",
"description": "kt-node",
"scripts": {
"start": "node ./out/production/kt-node/kt-node.js" //这里改成你编译后文件的位置
},
"author": "laziji",
"dependencies": {
"express": "^4.15.4",
"kotlin": "^1.1.4",
"mongoose": "^4.11.7"
}
}
1
2
$ npm install
$ npm start

打开localhost:8888 查看效果

若有报错

如果运行的时候报错
打开project settings -> Kotlin Complier
Module kind 改为 UMD 再尝试编译 运行