0

我正在尝试使用 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/mainsrc/test文件夹

创建src/kotlinsrc/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}")
}

kotlinnpm本地版本安装:注意npm i kotlin :全局安装npm i -g kotlin根本不起作用。

注意:我也使用npm installnpm 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 buildindex.js根据需要生成文件。

一旦运行该文件node index.js,它就不起作用了,并且看起来应用程序没有读取porthost调用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
4

1 回答 1

1

我刚刚找到它,它正在调用中server.listen

将语法视为“server.listen(端口、主机名、积压、回调);”

其中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然后我使用命令顺利运行它

于 2018-02-17T12:16:34.500 回答