diff --git a/README.md b/README.md index 11ecdb0..2dc4af4 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ To know more about Kotlin wasm, please visit https://kotl.in/wasm. * [Compose Jetsnack](compose-jetsnack#jetsnack) — Web version of [Jetsnack application](https://github.com/android/compose-samples/tree/main/Jetsnack) built with Compose Multiplatform * [Compose Image Viewer](compose-imageviewer#compose-multiplatform-for-web) — Web version of Image Viewer application built with Compose Multiplatform * [Browser Example](browser-example#kotlinwasm-browser-example) -- A simple app showing "Hello World" in the browser using DOM API. -* [Node.js Example](nodejs-example#kotlinwasm-nodejs-example) -- A simple app printing "Hello World" in the Node.js. +* [Node.js Example](nodejs-example#kotlinwasm-nodejs-example) -- A simple app printing "Hello World" and making a HTTP request in the Node.js. * [KoWasm](https://github.com/kowasm/kowasm) -- Server-side and full stack development with Kotlin and WebAssembly leveraging WASI and Component Model. * [WASI Example](wasi-example#kotlinwasm-wasi-example) -- A simple app using WASI API in the Node.js and Deno. diff --git a/nodejs-example/README.md b/nodejs-example/README.md index 2362d1e..60872c8 100644 --- a/nodejs-example/README.md +++ b/nodejs-example/README.md @@ -1,6 +1,6 @@ # Kotlin/Wasm Node.js Example -A simple app printing "Hello World" in the Node.js. +A simple app printing "Hello World" and making a HTTP request in the Node.js. > **Note** > It uses a nightly version of Node.js 20 diff --git a/nodejs-example/src/wasmJsMain/kotlin/Main.kt b/nodejs-example/src/wasmJsMain/kotlin/Main.kt index a7a5e00..762d271 100644 --- a/nodejs-example/src/wasmJsMain/kotlin/Main.kt +++ b/nodejs-example/src/wasmJsMain/kotlin/Main.kt @@ -1,3 +1,27 @@ +import kotlin.js.* +import org.w3c.fetch.* + +external fun fetch(input: String, init: JsAny = definedExternally): Promise + fun main() { println("Hello World!") -} \ No newline at end of file + + fetch("https://httpbin.org/get") + .then { + if (it.ok) { + it.text().then { + println() + println("HTTP response:") + println(it) + null + } + } else { + println("HTTP error: " + it.status) + } + null + } + .catch { + println("Error making HTTP request: " + it) + null + } +}