我正在尝试使用 Kotlin 创建一个简单的节点服务器来复制这里的基本示例:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
所以,我做了以下事情:
新建文件夹,命名为node
npm package
使用创建npm init
gradle project
使用此命令创建新的:gradle init --type java-library
删除src/main
和src/test
文件夹
创建src/kotlin
和src/resources
文件夹
将文件内容替换为build.gradle
:
buildscript {
ext.kotlin_version = '1.2.21'
ext.node_dir = 'node'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin2js'
repositories { jcenter() }
dependencies {
def kotlinx_html_version = "0.6.8"
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
}
sourceSets.main {
kotlin.srcDirs += 'src/kotlin'
resources.srcDirs += 'src/resources'
}
compileKotlin2Js.kotlinOptions {
outputFile = "${projectDir}/${node_dir}/index.js"
moduleKind = "commonjs"
sourceMap = true
}
clean.doFirst() {
delete("${node_dir}")
}
kotlin
从npm
本地版本安装:注意npm i kotlin
:全局安装npm i -g kotlin
根本不起作用。
注意:我也使用npm install
或npm i
在将依赖项添加到package.json
文件之后执行此操作,如下所示:
{
"name": "kotlin-node-app",
"version": "1.0.0",
"description": "Node Application built with Kotlin Lang",
"main": "index.js",
"dependencies": {
"kotlin": "^1.2.21"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"kotlin"
],
"author": "Hasan Yousef",
"license": "ISC"
}
创建我的文件src/kotlin/Main.Kt
如下:
external fun require(module:String):dynamic
fun main(args: Array<String>) {
println("Hello JavaScript!")
val http = require("http")
val hostname = "127.0.0.1"
val port = 3000
println("Server will try to run at http://${hostname}:${port}/")
val server = http.createServer{req, res ->
res.statusCode = 200
res.setHeader("Content-Type", "text/plain")
res.end("Hello World\n")
}
server.listen{port, hostname ->
println("Server running at http://${hostname}:${port}/")
}
}
使用构建项目gradle build
并index.js
根据需要生成文件。
一旦运行该文件node index.js
,它就不起作用了,并且看起来应用程序没有读取port
和host
调用server.listen
.
该应用程序的结构、代码和输出在此屏幕截图中:
index.js
生成的是:
(function (_, Kotlin) {
'use strict';
var println = Kotlin.kotlin.io.println_s8jyv4$;
var Unit = Kotlin.kotlin.Unit;
function main$lambda(req, res) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
return res.end('Hello World\n');
}
function main$lambda_0(port, hostname) {
println('Server running at http://' + hostname + ':' + port + '/');
return Unit;
}
function main(args) {
println('Hello JavaScript!');
var http = require('http');
var hostname = '127.0.0.1';
var port = 3000;
println('Server will try to run at http://127.0.0.1:3000/');
var server = http.createServer(main$lambda);
server.listen(main$lambda_0);
}
_.main_kand9s$ = main;
main([]);
Kotlin.defineModule('index', _);
return _;
}(module.exports, require('kotlin')));
//# sourceMappingURL=index.js.map