Executing PHP Scripts in Node.js Web Server
Question: Can Node.js web servers execute PHP scripts like Apache does, integrating PHP within Node.js?
Answer:
While directly executing PHP scripts in Node.js is not recommended, there are methods to integrate them through external mechanisms.
Option 1: Shell Invocation
To avoid directly executing PHP scripts in Node.js, you can invoke the PHP interpreter through the shell interface:
var exec = require("child_process").exec; app.get('/', function(req, res) { exec("php index.php", function (error, stdout, stderr) { res.send(stdout); }); });
Option 2: Web Server Relay
If you prefer not to execute PHP scripts directly in Node.js, you can relay them to another web server that handles PHP execution. One way to achieve this is through the following code:
var exec = require("child_process").exec; app.get('/', function(req, res) { exec("wget -q -O - http://localhost/", function (error, stdout, stderr) { res.send(stdout); }); });
The above is the detailed content of Can Node.js Web Servers Execute PHP Scripts Like Apache?. For more information, please follow other related articles on the PHP Chinese website!