Skip to content

Commit

Permalink
sdk/js: cleanup example a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed Jun 13, 2020
1 parent 671217b commit 22a6521
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions ts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@ import {Flatend} from "./flatend.js";
import {ID} from "./packet.js";
import ip from "ip";

const getTodos = data => {
data = JSON.parse(data);

const id = parseInt(data?.params?.id);
if (id !== 123) {
throw new Error(`ID must be 123. Got ${id} instead.`);
}

return data;
}

async function main() {
const keys = nacl.sign.keyPair();
const id = new ID({publicKey: keys.publicKey, host: "127.0.0.1", port: 12000});

const backend = new Flatend({id, keys});

backend.register("get_todos", data => {
data = JSON.parse(data);

if (parseInt(data?.params?.id) !== 123)
throw new Error(`ID must be 123. Got ${data?.params?.id}.`);

return data;
});

backend.register("all_todos", () => "hello world!");
backend.register("get_todos", getTodos);

await backend.start({port: 9000, host: "127.0.0.1"});


console.log(`Successfully connected to ${ip.toString(backend.id.host, 12, 4)}:${backend.id.port}.`);
}

Expand Down

0 comments on commit 22a6521

Please sign in to comment.