Here’s the scenario I anticipate: I have an app written in PHP which has a domain layer (complete with validators, etc). I want to use node.js for my web services for performance in high concurrency situations. If I create a command line interface for my php domain layer (see below), will this give me better performance than just using Apache? Is this even a good way to do this? I’m new to node.js and am trying to get my bearings. Node: The command line interface for the domain layer will return json encoded objects.
//Super simple example:
var http = require("http");
var exec = require('child_process').exec;
function onRequest(request, response) {
exec("php domain-cli.php -model Users -method getById -id 32", function(error, stdout, stderr){
response.writeHead(200, {"Content-Type": "application/json"});
response.write(stdout);
response.end();
});
}
http.createServer(onRequest).listen(80);
You would have to measure it to be sure, but I highly doubt it.
Node’s performance benefits come because it is a
select()-based server, so it eschews threading and blocking (with expensive context switches and CPU pipeline stalls) in favor of non-blocking IO (aka green threading). If you offload all the work to PHP, then you’re basically just using Node as a front-end server — at which point you should just use Apache, since mod_php will be doing almost exactly what you’re doing. Only mod_php can do it better, because it can keep the PHP interpreter hot in memory, instead of having to spin up a new interpreter on every request like you’re doing.Essentially what you’ve done is reimplement CGI using Node. So I would say no — if CGI is what you want, there are plenty of existing implementations out there!