# Creating simple node server with Kotlin

**URL:** <https://discuss.kotlinlang.org/t/creating-simple-node-server-with-kotlin/6671>\
**Category:** JavaScript\
**Created:** [February 17, 2018, 11:15am UTC](https://discuss.kotlinlang.org/t/creating-simple-node-server-with-kotlin/6671 "2018-02-17T11:15:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasan.oryx](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hasan.oryx/32/5515_2.png) [@hasan.oryx](https://discuss.kotlinlang.org/u/hasan.oryx)\
**Post date:** [February 17, 2018, 11:15am UTC](https://discuss.kotlinlang.org/t/creating-simple-node-server-with-kotlin/6671/1 "2018-02-17T11:15:02Z")

</div>

I’m trying to create simple `node server`, using `kotlin` lang, that is replicating the basic sample [here](https://nodejs.org/en/about/):

```
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}/`);
});

```

So, I did the below:

Created new folder, named it node

Created new `gradle project` using this command: `gradle init --type java-library`

Deleted the `src/main` and the `src/test` folders

Created the `src/kotlin` and `src/resources` folders

Replaced the content of the `build.gradle` file by:

```
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}")
}

```

Installed `kotlin` from `npm` as local version: `npm i kotlin`  
Note: installing it globally `npm i -g kotlin` did not work at all.

Created my file at `src/kotlin/Main.Kt` as below:

```
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}/")
    }
}

```

Built the project using `gradle build` and got the `index.js` file generated as required.

Once running the file `node index.js` it did not work, and looks the app not reading the values of `port` and `host` upon calling the `server.listen`.

The structure, code and output of the app are in this screenshot:

The `index.js` generated is:

```
(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

```

---

<div class="post-metadata">

**Author:** ![hasan.oryx](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hasan.oryx/32/5515_2.png) [@hasan.oryx](https://discuss.kotlinlang.org/u/hasan.oryx)\
**Post date:** [February 17, 2018, 12:21pm UTC](https://discuss.kotlinlang.org/t/creating-simple-node-server-with-kotlin/6671/2 "2018-02-17T12:21:08Z")

</div>

Just found it, the correct code worked with me is:

```
server.listen(port, hostname, fun() { 
    println("Server running at http://${hostname}:${port}/")
})

```

---

<div class="post-metadata">

**Author:** ![brett.wooldridge](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/brett.wooldridge/32/2335_2.png) [@brett.wooldridge](https://discuss.kotlinlang.org/u/brett.wooldridge)\
**Post date:** [February 18, 2018, 6:05am UTC](https://discuss.kotlinlang.org/t/creating-simple-node-server-with-kotlin/6671/3 "2018-02-18T06:05:45Z")

</div>

I _think_ you can use a lambda expression.

```
server.listen(port, hostname, () ->
    println("Server running at http://${hostname}:${port}/"))

```

Can you try it?

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 18, 2018, 7:03pm UTC](https://discuss.kotlinlang.org/t/creating-simple-node-server-with-kotlin/6671/4 "2018-02-18T19:03:29Z")

</div>

Your syntax for lambda expressions is wrong. I think you used the Java syntax(I don’t use Java much so I’m not sure). The Kotlin version would be

```nohighlight
server.listen(port, hostname, {
    println("Server running at http://${hostname}:${port}/") } )

```

If the lambda is the last argument you can also write

```nohighlight
server.listen(port, hostname) {
    println("Server running at http://${hostname}:${port}/") }

```

This version would be the idomatic way of writing this.

That said, there are some advantages to using anonymous functions as @hasan.oryx did. You can use return in them for example. Even though I would use a lambda expression in this case.

---

<div class="post-metadata">

**Author:** ![hasan.oryx](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hasan.oryx/32/5515_2.png) [@hasan.oryx](https://discuss.kotlinlang.org/u/hasan.oryx)\
**Post date:** [February 18, 2018, 7:07pm UTC](https://discuss.kotlinlang.org/t/creating-simple-node-server-with-kotlin/6671/5 "2018-02-18T19:07:42Z")

</div>

This way did not work with me, thanks for the feedback.

---

<div class="post-metadata">

**Author:** ![hasan.oryx](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hasan.oryx/32/5515_2.png) [@hasan.oryx](https://discuss.kotlinlang.org/u/hasan.oryx)\
**Post date:** [February 18, 2018, 7:13pm UTC](https://discuss.kotlinlang.org/t/creating-simple-node-server-with-kotlin/6671/6 "2018-02-18T19:13:20Z")

</div>

Thanks, but looks you have extra `)` at the end of this:

> [@Wasabi375](#):
>
> server.listen(port, hostname) {  
> println(“Server running at http://${hostname}:${port}/”) } )

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 18, 2018, 7:14pm UTC](https://discuss.kotlinlang.org/t/creating-simple-node-server-with-kotlin/6671/7 "2018-02-18T19:14:40Z")

</div>

Yup, your right. Changing it right now 😉
