1

This isn't for anything serious, more so to just play around and experiment. Just not sure where I would start. Also not bound to Node.js, just figured it's the most popular server-side JS tool around.

Though if there are some major technical reasons why one shouldn't do something like this on a real site, feel free to throw them out there.

What I'm asking about is something like…

$input = $js_string_built_via_php;

if ($run_on_server)
     $output = node.js($input);
else
     $output = '<script type="text/javascript">'.$input.'</script>';

echo $output;
Serhiy
  • 2,505
  • 3
  • 33
  • 49
  • You want node to compile and execute a PHP-generated string of JS? – Fabrício Matté Jan 10 '13 at 21:46
  • Yep, and give it back to php to toy with. – Serhiy Jan 10 '13 at 21:46
  • It is an interesting concept. To make things easier, I'd keep node running in the server then send a cURL with the JS string for node to execute it. Not sure how you'd pass the script contents through command-line, wait for a response and kill the process afterwards otherwise. – Fabrício Matté Jan 10 '13 at 21:49
  • Thanks, do you happen to know if there a limit to how long of a string one can pass through command-line? – Serhiy Jan 10 '13 at 21:52
  • I guess that depends on OS. Here's a generic question with some [useful answers](http://stackoverflow.com/a/6849345/1331430) about the max length of arguments. – Fabrício Matté Jan 10 '13 at 21:57
  • Of course, you could write to a JS file with php then `exec` it with `node myscript.js` to bypass those restrictions, but then I have no idea how you would fetch the output. – Fabrício Matté Jan 10 '13 at 21:57
  • Thanks for the info. I'm using Ubuntu, so if I can get a meg into it, that's far more than enough. I haven't done any command line calls out of PHP besides ImageMagick. You don't happen to know if a command line argument receive a value back? – Serhiy Jan 10 '13 at 22:11
  • If I go the read/write route, I imagine I could read a file back in PHP when Node.js is done. In which case I imagine I should probably use a RAM disk to speed things up? – Serhiy Jan 10 '13 at 22:20
  • I imagine it is possible. Sorry my skills with php and node aren't really up to the task, but the question is pretty interesting from an experiment viewpoint. – Fabrício Matté Jan 10 '13 at 22:23
  • n/p, thanks for the ideas, if I come up with anything I'll be sure to update this question – Serhiy Jan 10 '13 at 22:26

2 Answers2

6

There are several alternatives to using node here.

Have you seen the V8js PHP extension, which integrates the V8 engine with PHP? I don't have any experience with using it myself, though.

Alternatively, similar to using node, you could install Rhino (available in the Ubuntu repos, at least from 12.04). Or another command line javascript interpreter. Then you can execute javascript from the shell:

rhino -e '<javascript code>';

So you should be able to do something like the following. You would have to use print to output data and then parse the output back in php somehow:

<?php

function execute_js($script) {
    return shell_exec('rhino -e '.escapeshellarg($script));
}

$javascript = "
    function add(a,b) {
        return a+b;
    }
    print(add(5,6));
";

$result = execute_js($javascript);

print $result;

I doubt this would be a good idea in a production application and seems like it might be quite vulnerable with a much greater attack surface. With clients being able to inject javascript code that actually gets executed on the server. It's probably very slow at load also.

bencoder
  • 836
  • 5
  • 13
  • The V8js PHP extension looks exactly what I hoped for. Thanks! Wasn't aware of this. I was thinking of possibly using it more for things like form input validation so as to not have to write duplicate code to check for valid e-mail string, etc... Also JS based templating on the server-side, like mustache, for the web crawlers seemed like a cool experiment. – Serhiy Jan 14 '13 at 18:32
0

A better solution 3 years later?

dnode-php would a better solution for what you want today.

There is a cool introduction here. As mentioned by the author Henri Bergius:

DNode is a remote method invocation protocol originally written for Node.js, as the name probably tells. But as the protocol itself is quite simple, just sending newline-terminated JSON packets over TCP connections, implementations have started popping up in other languages. You can talk DNode in Ruby, Perl, Python, Java, and now PHP.

Mick
  • 30,759
  • 16
  • 111
  • 130