Calling node.js script from PHP returns nothing

What I want to do is just theoretically, but I can't get it to work.

I wrote a simple node.js script that uses the request package to asynchronously retrieve some data, parse it and spit it out as html. I wanted to integrate this script on my client php and apache based website, which is on a shared host, and ran into some errors:

  • There is no mod_proxy, so I can’t just run my node script as a server and proxy through Apache
  • I do not want to run node on port 80 and the proxy for apache with node. It just overwhelms too much what I need to do, and will introduce too many headaches for me. It is known that my specific shared host has problems with the content of the node server instances, and I cannot justify the possible downtime just for this script to run.
  • I tried the node-cgi package, but that did not work for me. I got errors regarding internal node methods that do not exist, I think this package is just deprecated.

So I landed trying to just call node from PHP. My whole index.php file:

 <?php header("Content-Type: text/html"); exec("node beerlist.nd", $output); echo implode('', $output); 

When I execute php index.php on the command line, I get the expected result, however, when I try to access this from the browser, I get nothing, i.e. Content-Length: 0 . Why?

I thought that maybe this is due to the asynchronous nature of my node script, but it looks like the process remains valid until it completes all asynchronous calls. So shouldn't my php fragment send output to the browser without any problems? What am I missing here?

Edit: this is the meaning of my node script

 var req = require('request') req("http://mywebsite.com", function(err, resp, body) { var output = // pull some interesting pieces out of the whole body console.log(output); }); 

Generating my output variable is not central to this problem. I want to use request for an asynchronous call and use console.log to output my results ... maybe this is a problem?

+7
javascript php apache
source share
1 answer

I assume that the Apache user does not know what the node command is. If I'm right, try writing in a php file:

 <full path to node> beerlist.nd 

instead

 node beerlist.nd 

To get the full path to the node, run in the terminal which node

+7
source share

All Articles