Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended a nodejs-example to make a HTTP request #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion nodejs-example/README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 25 additions & 1 deletion nodejs-example/src/wasmJsMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
import kotlin.js.*
import org.w3c.fetch.*

external fun fetch(input: String, init: JsAny = definedExternally): Promise<Response>

fun main() {
println("Hello World!")
}

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
}
}