GitBook: [2.0.0] 3 pages modified

This commit is contained in:
boneyard93501 2021-09-10 07:58:16 +00:00 committed by gitbook-bot
parent ddaaf7cda0
commit e75a597bae
No known key found for this signature in database
GPG Key ID: 07D2180C7B12D0FF
3 changed files with 2 additions and 126 deletions

View File

@ -19,7 +19,7 @@
* [Concepts](js-sdk/1_concepts.md) * [Concepts](js-sdk/1_concepts.md)
* [Basics](js-sdk/2_basics.md) * [Basics](js-sdk/2_basics.md)
* [In-depth](js-sdk/3_in_depth.md) * [In-depth](js-sdk/3_in_depth.md)
* [Running app in nodejs](js-sdk/4_run_in_browser.md) * [Running app in nodejs](js-sdk/5_run_in_node.md)
* [Running app in browser](js-sdk/4_run_in_browser-1.md) * [Running app in browser](js-sdk/4_run_in_browser-1.md)
* [Api reference](js-sdk/6-reference.md) * [Api reference](js-sdk/6-reference.md)
* [Changelog](js-sdk/changelog.md) * [Changelog](js-sdk/changelog.md)

View File

@ -1,2 +0,0 @@
# Running app in nodejs

View File

@ -1,124 +1,2 @@
# Intro # Running app in nodejs
JS SDK makes it easy to run applications in NodeJS environment. You can take full advantage of the javascript ecosystem and at the save time expose service to the Fluence Network. That makes is an excellent choice for quick prototyping of applications for Fluence Stack.
# Calc app example
Lets implement a very simple app which simulates a desk calculator. The calculator has internal memory and implements the following set of operations:
- Add a number
- Subtract a number
- Multiply by a number
- Divide by a number
- Get the current memory state
- Reset the memory state to 0.0
First, let's write the service definition in aqua:
```
-- service definition
service Calc("calc"):
add(n: f32)
subtract(n: f32)
multiply(n: f32)
divide(n: f32)
reset()
getResult() -> f32
```
Now write the implementation for this service in typescript:
```typescript
import { FluencePeer } from "@fluencelabs/fluence";
import { krasnodar } from "@fluencelabs/fluence-network-environment";
import { registerCalc, CalcDef, demoCalculation } from "./_aqua/calc";
class Calc implements CalcDef {
private _state: number = 0;
add(n: number) {
this._state += n;
}
subtract(n: number) {
this._state -= n;
}
multiply(n: number) {
this._state *= n;
}
divide(n: number) {
this._state /= n;
}
reset() {
this._state = 0;
}
getResult() {
return this._state;
}
}
const keypress = async () => {
process.stdin.setRawMode(true);
return new Promise<void>((resolve) =>
process.stdin.once("data", () => {
process.stdin.setRawMode(false);
resolve();
})
);
};
async function main() {
await FluencePeer.default.init({
connectTo: krasnodar[0],
});
registerCalc(new Calc());
console.log("application started");
console.log("peer id is: ", FluencePeer.default.connectionInfo.selfPeerId);
console.log(
"relay is: ",
FluencePeer.default.connectionInfo.connectedRelays[0]
);
console.log("press any key to continue");
await keypress();
await FluencePeer.default.uninit();
}
main();
```
As you can see all the service logic has been implemented in typescript. You have full power of npm at your disposal.
Now try running the application:
```bash
> node -r ts-node/register src/index.ts
application started
peer id is: 12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3
relay is: 12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e
press any key to continue
```
And the service can be called from aqua. For example:
```bash
const peer ?= "12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3"
const relay ?= "12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e"
func demoCalculation() -> f32:
on peer via relay
Calc.add(10)
Calc.multiply(5)
Calc.subtract(8)
Calc.divide(6)
res <- Calc.getResult()
<- res
```