Open Source API Gateway
2. Enterprise Integration Patterns

javascript

Version

Executes a Javascript. The script can access and manipulate data from the request and response. Use this or the Groovy plugin to extend the functions of Membrane by scripting. An object or array returned by the script, e.g. ({id: 7}) or [1, 2], becomes the JSON body of the message.

Every execution of the script runs isolated: variables are not shared with other requests. Objects created by the script, e.g. {a: 1} or the result of JSON.parse(), are only valid while the script runs. To keep such an object on the exchange, store it as a string: property.put('order', JSON.stringify(order)).

The variable log is an SLF4J logger named com.predic8.membrane.javascript. See the tutorials in tutorials/scripting.

Example Configuration

api:
port: 2000
flow:
- response:
- javascript:
src: |
var body = JSON.stringify({
foo: 7,
bar: 42
});


Response.ok(body).contentType("application/json").build();
- return: {} # Do not forward, return immediately
api:
   port: 2000
   flow:
     - response:
         - javascript:
             src: |
               var body = JSON.stringify({
               foo: 7,
               bar: 42
               });

               Response.ok(body).contentType("application/json").build();
     - return: {} # Do not forward, return immediately

Syntax

javascript:
location: <string>
src: <string>
javascript:
  location: <string>
  src: <string>

Attributes

NameRequiredDefaultDescriptionExamples
locationfalseN/AA file or URL location where the content that should be set as body could be foundconf/body.txt
srcfalse---

Can be used in