douwei8672 2012-10-23 06:39
浏览 46

在node.js套接字服务器脚本中运行php代码

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?

  • 写回答

3条回答 默认 最新

  • download12749 2012-10-23 09:53
    关注

    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('
    ');
        });
    
        // 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)

    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题