What I want to do is simple in theory, but I cannot quite get it to work.
I wrote a simple node.js script that uses the request
package to asynchronously fetch some data, parse it, and spit it out as html. I wanted to integrate this script in my client's php and apache based website which is on a shared host, and ran into some snags:
node-cgi
package but it didn't work for me. I got errors about internal node methods not existing, I think this package is just out of date.So what I have landed on is trying to simply call node from PHP. My whole index.php
file is:
<?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 my expected output, however, when I try to access this from the browser, I get nothing ie Content-Length: 0
. Why?
I thought maybe it had to do with the async nature of my node script but it seems like the process stays alive until it finishes all the async calls. So shouldn't my php snippet send the output to the browser without any trouble? What am I missing here?
Edit: This gist of my node script is
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);
});
The generation of my output
variable is not central to the issue here. The relevant parts are that I use request
to make an asynchronous call and use console.log
to output my results... maybe this is a problem?
I suppose Apache user doesn't know what node
command is. If I'm right try to write in php file:
<full path to node> beerlist.nd
instead of
node beerlist.nd
To get full path to node run in terminal which node
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With