Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including moleculer-cls with all npm packages installed. Try it out:

require("moleculer/package.json"); // moleculer is a peer dependency. var moleculerCls = require("moleculer-cls")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

moleculer-cls v1.0.0

Hooked Continuation-Local Storage for Moleculer

Hooked Continuation-Local Storage for Moleculer

Add support for continuation local storage in moleculer action handlers.

Current Version (v1.0.0)

Install

npm install --save moleculer-cls

Usage

// moleculer.config.js

const context = require('moleculer-cls');

module.exports = {
  ...
  middlewares: [
    context.middleware
  ]
}
// service.js

const context = require('moleculer-cls');
const process = require('processor.js');

module.exports = {
  actions: {
    hello: {
      params: {
        properties: {
          "name": {type: "string"}
        },
        required: ["name"]
      },
      handler: async (ctx) => {
        context.set('request_id', ctx.requestID);
        
        return await process(ctx.params);
      }
    }
  }
}
// processor.js

const context = require('moleculer-cls');

module.exports = async ({name}) => {
  console.log(`Processing ${context.get('request_id')}`);

  return Promise.resolve(`Hello ${name}`);
}

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free