Skip to main content
Skip table of contents

NodeJS

NodeJS component can be used to launch NodeJS scripts. This component provides two modes of operations, server and client, facilitating to run scripts written in both these modes. Communication between java and NodeJS is done using Sockets. A socket server is hosted in the NodeJS part which communicates with the socket client in java.

Configuration

Component Configuration panel in the CPS (Configuration Property Sheet) allows customization in the component.

 


Figure 1: NodeJS Configuration properties

Attributes

Error handling configuration

Please refer to the Error Handling section in the Common Configurations page for documentation.

Is Script from File? 

Select this property if the script is provided by the file path.

Script Path 

Provide the path of the file which contains the Node JS Script.

For Custom modules imported in the script, the fully classified path should be mentioned. Socket HOST and PORT should be the same as CPS values.

Script 

Available when 'Is Script from File? property is disabled. Provide the contents of the Node JS Script. Template for NodeJS script in Server and Client mode has been preloaded.

In the script for imports other than native modules full path of the module location is to be provided. HOST and IP values for socket Server should be the same as CPS properties 'Socket IP' and 'Socket Port' respectively.

To easily download npm-modules have node JS installed with NPM. This link provides instructions to install it. 

https://nodejs.org/en/download/package-manager/

Operation Mode

  • Server : This mode can be used to host a nodeJS server. Only the output port is available in this configuration to receive responses generated by the server.
  • Client: This mode can be used to host a nodeJS client. Both input and output ports are available in this configuration to make a request and receive a response.
Socket IP 

The IP address of the socket server given in Node JS script.

Socket Port

The port of the socket server given in Node JS script.

Script Startup Time(milliseconds)

This option is available only in server mode. The script needs some time to start the socket server to which a socket connection should be made, so a timeout is needed. However, this timeout should have a threshold. Thus the component waits for a maximum of this time for the Socket Server to start before stopping.

Socket Response Time-Out(milliseconds)

This option is available only in client mode. When a request is sent to the socket a response should be received within this time. Timeout value '0' means to wait indefinitely.

Expert Properties

Enable the View Expert Properties option to configure these properties. 

Expert properties are meant for advanced users. Use with caution!


Figure 2: NodeJS Configuration advanced properties

Pre Processing XSL Configuration

Pre Processing XSL configuration can be used to transform the request message before processing it. Click the ellipses button against the property to configure the properties.
Refer to the Pre/Post Processing XSL Configuration section under the Common Configurations page for details regarding Pre Processing XSL configuration and Post Processing XSL configuration (below).

Post Processing XSL Configuration

Post Processing XSL configuration can be used to transform the response message before sending it to the output port.

Process Message Based on Property

The property helps components to skip certain messages from processing.
Refer to the Process Message Based On a Property section under the Common Configurations page.

Threadpool Configuration

This property is used when there is a need to process messages in parallel within the component, still maintaining the sequence from the external perspective.
Refer to the Threadpool Configuration section under the Common Configurations page.

Functional Demonstration

Scenario

Configure two Node JS components, one as Server and another as Client each in two separate EPs. 

The flow and scripts for client and server mode are provided below. An HTTP POST request is made to the server from the client via input port. The socket connection in the client is used to send a request and the response is obtained from the server. The output from the server is given out in the output port. This response is propagated to the client which is sent out through its output port.


Figure 3: Server mode event process

Click here for a sample Server script

CODE
Server.js
var http = require('http');
var net = require('net');
var propagate = Buffer.from('trial');
var querystring = require('C:
Program Files\\nodejs\\node_modules
querystring');
var server = http.createServer().listen(7000); 
var HOST = '127.0.0.1';
var PORT = 7989;
// Create an instance of the Server and waits for a connection
net.createServer(function(sock) {
// Receives a connection - a socket object is associated to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress ':' sock.remotePort);
// Add a 'data' - "event handler" in this socket instance
propagate = sock;
sock.on('data', function(data) {
// data was received in the socket
// Writes the received message back to the socket (echo)
//sock.write(data);
});
// Add a 'close' - "event handler" in this socket instance
sock.on('close', function(data) {
// closed connection
console.log('CLOSED: ' + sock.remoteAddress ' ' sock.remotePort);
});
/*sock.on('error', function() {
// closed connection
console.log('CLIENT HAS ERRORED: ');
});*/
}).listen(PORT, HOST);
console.log('Socket Server listening on ' + HOST ':' PORT); 
server.on('request', function (req, res) {
if (req.method == 'POST') {
var body = '';
}
req.on('data', function (data) {
body += data;
//Request Listener
if(propagate.readyState!='closed'){ //prevents trying to write when Socket is closed from client end
propagate.write(data);
propagate.write('\n');
//console.log(propagate.readyState);
}
});
req.on('end', function () {
var post = querystring.parse(body);
console.log(post);
res.writeHead(200, {'Content-Type': 'text/plain'});
//Response Listener
if(propagate.readyState!='closed'){ //prevents trying to write when Socket is closed from client end
propagate.write(Buffer.from(JSON.stringify(post)));
propagate.write('\n');
}
res.end(JSON.stringify(post));
});
});
console.log('HTTP Server Listening on port 7000');  


Figure 4: Client mode event process

Click here for a sample Client script

CODE
Client.js
var http = require('http');
var net = require('net');
var querystring = require('C:
Program Files\\nodejs\\node_modules
querystring');
var propagate = new Buffer('trial');
var req = new Buffer('trial');
var postData = querystring.stringify({
msg: 'hello world'
});
var HOST = '127.0.0.1';
var PORT = 7979;
// Create an instance of the Server and waits for a connection
var sockserver = net.createServer(function(sock) {
// Receives a connection - a socket object is associated to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress ':' sock.remotePort);
// Add a 'data' - "event handler" in this socket instance
propagate = sock;
sock.on('data', function(data) {
// data was received in the socket
// Writes the received message back to the socket (echo)
postData = querystring.stringify({
msg: data.toString('utf8')
});
console.log(JSON.stringify(data)); 
//HTTP POST Request
var options = {
hostname: 'localhost',
port: 7000,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
//Define HTTP Request
req = http.request(options, function (res) {
console.log('STATUS:', res.statusCode);
console.log('HEADERS:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY:', chunk);
propagate.write(new Buffer(JSON.stringify(chunk)));
});
res.on('end', function () {
propagate.write('\n');
console.log('No more data in response.');
});
});
req.on('error', function (e) {
console.log('Problem with request:', e.message);
//In case of error socket should recieve response
propagate.write('Error \n');
});
var temp = req.write(querystring.stringify({
msg: data.toString('utf8')
}));
console.log('httpresponse:' +temp);
req.end();
});
// Add a 'close' - "event handler" in this socket instance
sock.on('close', function(data) {
// closed connection
console.log('CLOSED: ' + sock.remoteAddress ' ' sock.remotePort);
sockserver.close(function(){
console.log("Closing socket");
});
});
}).listen(PORT, HOST);
console.log('Server listening on ' + HOST ':' PORT);

 

CODE
var querystring = require('C:
Program Files\\nodejs\\node_modules
querystring');

The path in this line should be replaced with the path of the local 'querystring' module installation.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.