我正在尝试使用 Kotlin 创建一个简单的 Node 服务器来复制基本示例 here :
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 init
创建npm包
使用以下命令创建新的 gradle 项目
: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}")
}
从 npm
安装 kotlin
作为本地版本:npm i kotlin
注意:全局安装 npm i -g kotlin
根本不起作用。
注意:在将依赖项添加到 package.json
文件后,我还使用 npm install
或 npm i
完成了此操作,变得像:
{
"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
最佳答案
我刚刚找到了它,它在调用server.listen
考虑语法为“server.listen(port, hostname, backlog,callback);”
在 Kotlin
中应该是:
server.listen(port, hostname, fun() {
println("Server running at http://${hostname}:${port}/")
})
所以,下面的代码现在非常适合我:
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, fun() {
println("Server running at http://${hostname}:${port}/")
})
/* OR
server.listen(port, hostname, {
println("Server running at http://${hostname}:${port}/") } )
*/
/* OR
server.listen(port, hostname) {
println("Server running at http://${hostname}:${port}/") }
*/
}
生成的编译后的index.js
文件为:
(function (_, Kotlin) {
'use strict';
var println = Kotlin.kotlin.io.println_s8jyv4$;
function main$lambda(req, res) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
return res.end('Hello World\n');
}
function main$lambda_0() {
println('Server running at http://127.0.0.1:3030/');
}
function main(args) {
println('Hello JavaScript!');
var http = require('http');
var hostname = '127.0.0.1';
var port = 3030;
println('Server will try to run at http://127.0.0.1:3030/');
var server = http.createServer(main$lambda);
server.listen(port, hostname, main$lambda_0);
}
_.main_kand9s$ = main;
main([]);
Kotlin.defineModule('index', _);
return _;
}(module.exports, require('kotlin')));
//# sourceMappingURL=index.js.map
然后我使用node node/index.js
命令顺利运行它
关于javascript - 使用 Kotlin 创建简单的 Node 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48840635/