Search Results

Custom JavaScript Stage

Create your own stages using JavaScript and available Node.js modules if no out-of-the-box stage exists.

You can configure different custom stages for request and response pipelines. To​ ​add​ ​a​​ ​custom stage to request pipeline,​ ​select ​​Custom Request when​ ​adding a new stage​. To add a​ ​custom stage to the response pipeline, select Custom Response when adding a new stage. You can add multiple Custom JavaScript stages for the request or response pipelines.

Custom JavaScript stages have following features:

  • You can access the properties of the requests received from clients, or the properties of the response received from backend services, to customize the API behaviors. You can even add, delete or modify some of the properties for their special needs. See Request and Response Properties for more information.

  • The HTTP headers of both the request and response can be customized by using Custom Javascript stages. You can determine which headers to be forwarded from the client to the backend service, and vice versa; you can also add an additional header, change the value of a header or delete a header. See Custom Headers for more information.

  • You can create JavaScripts working in either synchronous or asynchronous mode. One of the JavaScript’s strength is the way it handles asynchronous (async) code. Instead of getting blocked, the thread is pushed in an event queue which gets fired after the execution of all the other codes. This means that you can let your code do several things at the same time without stopping or blocking your main thread. See Async Module for more information.

  • The following Node.js modules are available for use in the custom JavaScript. These modules are all available on NPM. You can contact TIBCO Support if you need the support for other Node.js modules to facilitate your custom JavaScript programming.

The following Node.js modules are safe to use within Microflow:

  • lodash
  • moment
  • q
  • jsonwebtoken
  • validator
  • uuid
  • compression
  • async
  • request
  • jsdom
  • xml2js
  • ws

Configurations

Custom JavaScript stage contains the following fields:

Field Description
Name Name of Custom JavaScript stage.
Description Description of Custom JavaScript stage.
JSON input In the request flow, this is the model schema transferred from the original request body of the operation. In the response flow, this is the model schema transferred from the original response of the operation.
Examples JavaScript code sample.
Custom code JavaScript code to process the request. In the request flow, the parameter passed to the custom JavaScript function are req and done, with the query, params, headers, payload, and info properties of the request object (the req parameter) exposed. In the response flow, the parameter passed to the custom JavaScript function are res and done, with the headers and body properties of the response object (the res parameter) exposed. See Request and Response Properties for more information. In both the request and response cases, done is an internal implemented callback function. It is used by the custom script for notifying the invoker of this custom script that all works have been done, so the control is returned back to the invoker. This done callback is invented to facilitate the asynchronous scripting mode, and is also used by synchronous scripting for consistency. See Async Module for more information.

Example

The following sample code adds a vendor parameter in the response of the operation GET /pet/findByStatus:

if (_.isArray(res.body)) {
	res.body = {
		raw: res.body,
		total: res.body.length,
		vendor: 'TIBCO'
	};
}

done(res);

After pushing the configured stage and running the Microflow app, query Microflow_endpoint/pet/findByStatus?status=sold, you can get the following response as shown.