Execute PHP scripts within Node.js web server

I had the same question. I tried invoking php through the shell interface, and it produced the desired result:

var exec = require("child_process").exec;
app.get("https://stackoverflow.com/", function(req, res){exec("php index.php", function (error, stdout, stderr) {res.send(stdout);});});

I’m sure this is not high on the recommended practices list, but it seemed to do what I wanted. If, on the other hand, you don’t want to execute PHP scripts directly from Node.js but want to relay them from another web server that does, this seems to do the trick:

var exec = require("child_process").exec;
app.get("https://stackoverflow.com/", function(req, res){exec("wget -q -O - http://localhost/", function (error, stdout, stderr) {res.send(stdout);});});

Leave a Comment