How can to create a JavaScript Hello World in a Mule component
mule, mule-component
Solution
Here is a simple example:
You send a JSON encoded array and the script will return you the sum. Simple!
There is the flow:
<flow name="calculateFlow1" doc:name="calculateFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8089" doc:name="HTTP"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<scripting:component doc:name="JavaScript">
<scripting:script engine="JavaScript">
<scripting:text><![CDATA[
var a = eval('(' + payload + ')');
for (var i = 0, sum = 0; i < a.length; sum += a[i++]);
message.setPayload(sum + "");
result = message;
]]></scripting:text>
</scripting:script>
</scripting:component>
</flow>
Variables already given by Mule: `message`, `payload`.
Problem
I am trying to build a simple script inside a Mule component but I can't seem to find any documentation on how to get me started. The JavaScript Component Reference shares no ideas on how to get something simple to run.