More than one node.js in a proxy?

Not applicable

The use of a node.js target endpoint is pretty straightforward, but it appears that due to the way a proxy is configured, it is only possible to have one node.js script in a proxy. Specifically, the package.json has a main field that must be filled in with the node.js to start. However, some of the documentation includes discussing how to have more than one node.js file in a proxy. How is this second node.js file executed? I have an application for a proxy containing two node.js components, one to act as the target of a proxy - the normal usage for node.js, and another that runs as a demon process - does not need to be attached to a flow at all but simply update records in collections continuously. Is this possible in a single proxy? How do I configure the second node.js application to run, or is it run automatically when the proxy is deployed?

Solved Solved
0 7 685
1 ACCEPTED SOLUTION

@Eric Hildum - it is indeed possible to have two node.js scripts in a proxy. You can implement two node.js files and conditionally route between them in target configuration.

app1.js (/resources/node/app1.js)

var http = require('http');

var svr = http.createServer(function(req, resp) {
    resp.end('Response from app1');
});

svr.listen(9000, function() {
    console.log('Node HTTP server is listening');
});

app2.js (/resources/node/app2.js)

var http = require('http');

var svr = http.createServer(function(req, resp) {
    resp.end('Response from app2');
});

svr.listen(9001, function() {
    console.log('Node HTTP server is listening');
});

target1.xml (/targets/target1.xml)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="target1">
    <ScriptTarget>
        <ResourceURL>node://app1.js</ResourceURL>
    </ScriptTarget>
</TargetEndpoint>

target2.xml (/targets/target2.xml)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="target2">
    <ScriptTarget>
        <ResourceURL>node://app2.js</ResourceURL>
    </ScriptTarget>
</TargetEndpoint>

default.xml (/proxies/default.xml)

<RouteRule name="target1">
        <Condition>request.queryparam.target = "1"</Condition>
        <TargetEndpoint>target1</TargetEndpoint>
</RouteRule>

<RouteRule name="target2">
        <Condition>request.queryparam.target = "2"</Condition>
        <TargetEndpoint>target2</TargetEndpoint>
</RouteRule>

Now if you execute this API with ?target=1, you will get:

Response from app1

If you execute with ?target=2, you will get:

Response from app2

View solution in original post

7 REPLIES 7

@Eric Hildum I'm not versed with the second nodejs in a single proxy. However we have executed similar requirements by just running a second dummy proxy with a nodejs target. The API does nothing and doesn't serve any requests. But you can run the NodeJS and do whatever process you'd like to be done in that.

Just spawn an express server to keep it alive and running and use a setInterval to keep the daemon job running.

I've seen this use case a few times @Prashanth Subrahmanyam. So, for instance a job scheduler executing a task every x minutes. One of my concerns with this approach is that you'll run multiple job schedulers in parallel, one for each MP, so managing concurrency might be an issue here.

Do you happen to know how to manage this scenario?

@Madhan Sadasivam, I remember you had this requirement for one of our customers. I'd like to hear your thoughts. In a simplistic form, this is how I'd implement it:

var express = require('express')
var app = express()


app.get('/', function (req, res) {
  res.send('Hello World!')
})


// serves http requests
var server = app.listen(3000, function () {


  var host = server.address().address
  var port = server.address().port


  console.log('Example app listening at http://%s:%s', host, port)


})


// prints 'test' every five seconds
var interval = setInterval(function(){
  console.log('test');
}, 5000);

@Eric Hildum - it is indeed possible to have two node.js scripts in a proxy. You can implement two node.js files and conditionally route between them in target configuration.

app1.js (/resources/node/app1.js)

var http = require('http');

var svr = http.createServer(function(req, resp) {
    resp.end('Response from app1');
});

svr.listen(9000, function() {
    console.log('Node HTTP server is listening');
});

app2.js (/resources/node/app2.js)

var http = require('http');

var svr = http.createServer(function(req, resp) {
    resp.end('Response from app2');
});

svr.listen(9001, function() {
    console.log('Node HTTP server is listening');
});

target1.xml (/targets/target1.xml)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="target1">
    <ScriptTarget>
        <ResourceURL>node://app1.js</ResourceURL>
    </ScriptTarget>
</TargetEndpoint>

target2.xml (/targets/target2.xml)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="target2">
    <ScriptTarget>
        <ResourceURL>node://app2.js</ResourceURL>
    </ScriptTarget>
</TargetEndpoint>

default.xml (/proxies/default.xml)

<RouteRule name="target1">
        <Condition>request.queryparam.target = "1"</Condition>
        <TargetEndpoint>target1</TargetEndpoint>
</RouteRule>

<RouteRule name="target2">
        <Condition>request.queryparam.target = "2"</Condition>
        <TargetEndpoint>target2</TargetEndpoint>
</RouteRule>

Now if you execute this API with ?target=1, you will get:

Response from app1

If you execute with ?target=2, you will get:

Response from app2

So the package.json file does not matter that much? No need to be concerned about setting the main property in that file? Great! Thanks.

Exactly. I don't even have a package.json file in my sample.

Interesting, the sample baas application you get when creating a proxy puts a package.json file in the proxy for you automatically. Surprising that it is not really needed. I'll delete it, thanks.

,

Yes, I do. Contact me directly. ehildum at constratus com.