Getting Started
Server SDKs
Node

Getting Started with Stanza in Node.js

Stanza provides an SDK implementation to integrate Stanza functionalities into a Node application.

Install the Stanza Node SDK

npm install @getstanza/node
 
# or
# yarn add @getstaza/node

Initialize Stanza

To initialize the Stanza SDK add the following script to your application eg. initStanza.js

import { init } from '@getstanza/node'
 
init({
  hubUrl: '<url-to-hub>', // eg. https://hub.stanzasys.co:9020
  apiKey: '<valid-api-key>', // can be set up in Stanza UI
  serviceName: '<service-name>', // eg. DemoCommerce (needs to be configured in Stanza UI)
  serviceRelease: '<service-release>',
  environment: '<environment>' // eg. local (needs to be configured in Stanza UI)
})

Stanza Node SDK needs to be initialized before you load any other scripts in order to work correctly.

  • You can either load it using -r parameter in node
node -r initStanza.js app.js
  • or import it at the beginning of you app file ie. app.js
require('./initStanza')
 
/*
  your app code
*/

Wrapping code with a guard

One of the main features of Stanza is the ability to guard the execution of your code by wrapping it with a guard. If you want to learn more about what guards are please read our glossary page first.

Let's imaging the following block of code:

console.log('About to call a service')
await aService.doSomething()
console.log('Service returned successfully')

If we want to guard this block of code with a Stanza guard we can first create and configure a guard in the stanza UI.

Then we can define it in code:

const myGuard = stanzaGuard({ guard: 'my-guard' })

And then we can wrap the code with this guard:

await myGuard.call(async () => {
  console.log('About to call a service')
  await aService.doSomething()
  console.log('Service returned successfully')
})

Now the code wrapped with a guard will only get executed if all checks enforced by a guard (requesting for quota, validating the incoming token etc.) are satisfied. Otherwise, it will reject with a StanzaError describing the cause of the execution being blocked.

As an alternative we can wrap an existing function, let's call it myFunction, and wrap it to be reused later.

async function myFunction() {
  console.log('My function starts')
  await doSomething()
  console.log('Service returned successfully')
}

In order to wrap it we can use the same guard instance as above, but use a bind method instead:

const myGuardedFunction = myGuard.bind(myFunction)

In this example we don't call the myFunction immediately but wrap it with another function that can be reused and called later

/*
  This will first verify all the conditions enforced by a guard and only execute `myFunction` if they are satisfied.
 */
myGuardedFunction()

Debug Mode

The Stanza Node.js SDK has a debug mode that can be enabled to get additional output. Set an environment variable:

STANZA_LOG_LEVEL=debug

The debug mode uses Pino under the hood. If you want fewer logs, you can also set the log level higher according to the pino (opens in a new tab) log levels.

;