Run php script and send arguments from node.js


I have a PHP script that receives 3 arguments to insert into a database

php app.php "Arg1" "Arg2" "Arg3"

When I run it from console I can do it without any problem, to call it from my node.js script I try with:

var exec = require("child_process").exec;
    app.get('/', function(req, res) {
        exec("php app.php", function(error, stdout, stderr) {
            console.log("ESPERO QUE NO TENGA ERROR: ", error);
            if (!error) {

That code does not do anything, I do not even find a way to send arguments to it. Do you know what is the correct way to run a php script and send the arguments from node.js?

asked by Ricardo Garcia 02.10.2018 в 02:01

1 answer


In all cases, I have seen the answer, which can not be done. Which is not quite correct. Since you can run PHP on the command line, and Node.js can easily run programs through the command line.

What are the benefits?

  • Easy collaboration between teams with different knowledge.
  • PHP is really good and easy to create and launch fast scripts
  • It is not necessary to run multiple servers if so much is needed Node.js and PHP

What are the drawbacks?

  • More possibilities for security weaknesses

  • Not very good performance

  • It's easy to shoot yourself in the foot

You would have to reinvent the wheel, that is to program the same methods $ _GET , $ _REQUEST , etc. This is because you would have to program your server, then ... I do not think you want to do this, there is a small team that works on it link

If you already know PHP and are learning Node, you probably have a tendency to program "hard" things in PHP, so you will not learn how to do it in node.


Of course, there are probably more disadvantages and benefits.

I leave you an article that details this part since it would be very cumbersome to put all this content here: Article

answered by 02.10.2018 в 05:16