3

I am running a normal TCP socket in javascript and uses node to execute. Before I send a response back to the client, I want to validate the data with some some php (mysql) code. How can I execute this code inside this javascript file? All similar questions in STACKOVERFLOW asks about this code in HTML. (which is okay and I understand that part), But this is a normal javascript file (executed in node). The javascript code is below and I've marked the validation function call.

var net = require('net');

var HOST = '192.168.0.6';
var PORT = 8765;

net.createServer(function(sock) {

    console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

    sock.on('data', function(data) {
        var output;
        var validScan;

        //This is the function call to a php file
        **validScan = validateScanTag(data);**

        console.log('DATA ' + sock.remoteAddress + ': ' + data);
        sock.write(validScan);

    });

    // Closing this instance of socket
    sock.on('close', function(data) {
        console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
    });

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

The validateScanTag executes a line lke :

$.post('getperiod.php', {hours:hrs, minutes:mins, seconds:secs, ddd:day},

But this dont work. How can I call this 'geteriod.php' file from a javascript file?

VolkerK
  • 95,432
  • 20
  • 163
  • 226
Brad
  • 87
  • 1
  • 7
  • @DainisAbols: Im lost. This does not make sense? How would renaming my php file make it to execute. The error I get says something like $. is not defined. – Brad Oct 23 '12 at 06:50
  • `normal javascript file (executed in node)` - you mean node.js as in http://nodejs.org/ ? – VolkerK Oct 23 '12 at 06:56
  • Please help us by explaining what `$.post` is based on (lib? looks like jQuery which wouldn't make sense here) and how the actual result of execution looks like (any errors?). As far as I understand you're trying to call a php script through a server-side http request using node. Therefore some kind of http server with a php handler must be able to answer this request. – matthias Oct 23 '12 at 08:32
  • I've used $.post (jQuery yes) on a webpage yes. The webpage did all this php (mysql) validation for me and it worked fine, but I want to move this validation to the server app in javascript. Now Im looking for a way to get the results from the php file into javascript. – Brad Oct 23 '12 at 08:52
  • How did you integrate jQuery with node.js? And once again: Any error output from your node.js application...? – matthias Oct 23 '12 at 08:59
  • The error I get is: "ReferenceError: $ is not defined". And no, I did notintegrate jQuery with node.js. What was a different webapp. Im trying to move it all to a clean js file and execute that on node. What I basically need to do is to access my database when the server app is running. And that I dont know how to do. I've read something about node.js-mysql, but cant find any examples about it. – Brad Oct 23 '12 at 09:12

3 Answers3

1

I'm pretty sure the problem itself is well worth rethinking (for example, port your mysql code from php to node) but here is my solution:

var net = require('net');
var PORT = 8765;
var Lazy=require('lazy');
var spawn = require('child_process').spawn;

// php code runs as a server in external process

var validator = spawn('php', ['validator.php']);

net.createServer(function(sock) {

    console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

    // on 'data' handler is not enough to read input line by line
    // you need to buffer data, use some kind of state machine or one of available stream parsers. I use lazy here: npm install lazy

    // write each line to php validation code
    new Lazy(sock).lines.forEach(function(line) {
        console.log('DATA ' + sock.remoteAddress + ': ' + line.toString());
        validator.stdin.write(line);
        validator.stdin.write('\n');
    });

    // read validation result line by line from validator process
    new Lazy(validator.stdout).lines.forEach(function(line) {
        console.log('Validation result:' + line.toString()) // line is Buffer object here, hence toString
    });

    validator.stdout.pipe(process.stdout);

    // Closing this instance of socket
    sock.on('close', function() {
        console.log('CLOSED');
    });

}).listen(PORT);

validator.php should read from stdin and write to stdout

<?

function validatescanTag($l) {
    // put your validation code here
    return $l . '!!!!';
}

$stdin = fopen('php://stdin', 'r');

do {
  $line = fgets($stdin);
  print validateScanTag($line);
} while(1);

?>

Another option is to use fastcgi protocol to communicate node <-> php or dnode rpc. Or just use http and run php from nginx/apache (or even node-php)

Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75
0

An approach to give a more generic answer for clarification and "getting you on the boots":

As far as I understand you're trying to run your php script using a http post request invoked by the node.js server.

You probably first have to become aware of the fact that running server-side JavaScript normally has different motivation and possibilities as well as impacts on your architecture than running JS inside a browser - at least in a classical "jQuery sense".

As stated in your comment you expect jQuery to work seamlessly within server-side node.js. That doesn't work as the comment-posted ReferenceError: $ is not defined shows you. There is no server-side jQuery available in your node.js stack working out-of-the-box (possibilities of using jQuery server-side should be discussed separately and is another subject).

Nevertheless you're obviously looking for a way to re-use your PHP script which already handles your mysql data connection and handling by utilizing it server-side. Doing this using http there are possibilites like shown in this stackoverflow post or in the http.request section of the node.js documentation. If your php script resides on the same server (or better runtime environment) as your node.js is running you may also directly execute the php script it by spawning it as a child process with the php standalone (command line) utility.

Last but not least: Yes, there are some ways (npm modules, ORM layers) for connecting mysql directly with your node.js app. But that's another complex subject and is discussed at other places.

I hope that helped in a way to avoid mixing up things that shouldn't be mixed up.

Community
  • 1
  • 1
matthias
  • 2,255
  • 1
  • 23
  • 28
  • Ah, okay - as I see @Andrey Sidorov provided a solution for spawning a php child process :). Hope, you get things up and running now. – matthias Oct 23 '12 at 10:19
0

There is a PHP extension embedding Chrome's V8 engine in PHP.

Andris
  • 4,392
  • 2
  • 20
  • 16