Removed upstream API
package com.baeldung.kotlinjs
-import kotlin.js.Json
-
external fun require(module: String): dynamic
+data class CryptoCurrency(var name: String, var price: Float)
+
fun main(args: Array<String>) {
println("Crypto Currency price API!")
-
val express = require("express")
- val request = require("request");
-
val app = express()
- //coin market api to fetch latest crypto currencies prices
- val url = "https://api.coinmarketcap.com/v2/ticker/?limit=10"
-
app.get("/crypto", { _, res ->
- request(url) { error: String?, _, body: String ->
- res.type("application/json")
- if (error.isNullOrEmpty()) {
- val data: dynamic = JSON.parse<Json>(body)
- res.send(data);
- } else {
- println(error)
- res.send(error);
- }
- }
-
+ println(generateCryptoRates())
+ res.send(generateCryptoRates())
})
app.listen(3000, {
println("Listening on port 3000")
})
}
+fun generateCryptoRates(): Array<CryptoCurrency>{
+ val crytoCurrency = arrayOf<CryptoCurrency>(CryptoCurrency("Bitcoin",
+ 90000F), CryptoCurrency("ETH",1000F),
+ CryptoCurrency("TRX",10F));
+ return crytoCurrency
+}
\ No newline at end of file
