在node.js套接字服务器脚本中运行php代码 [英] Running php code inside a node.js socket server script

查看:106
本文介绍了在node.js套接字服务器脚本中运行php代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中运行一个普通的TCP套接字并使用node来执行。在我将响应发送回客户端之前,我想用一些php(mysql)代码验证数据。
如何在这个javascript文件中执行此代码? STACKOVERFLOW中的所有类似问题都在HTML中询问此代码。 (这是好的,我理解那部分),但这是一个普通的javascript文件(在节点中执行)。 javascript代码如下,我已经标记了验证函数调用。

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);

validateScanTag执行一行lke:

The validateScanTag executes a line lke :

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

但是这不起作用。如何从javascript文件中调用这个'geteriod.php'文件?

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

推荐答案

我很确定问题本身值得重新思考(例如,将你的mysql代码从php移植到节点)但这是我的解决方案:

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应该从stdin读取并写入stdout

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);

?>

另一种选择是使用 fastcgi protocol 用于通信节点< - > php或 dnode RPC 。或者只使用http并从nginx / apache运行php(甚至 node-php

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)

这篇关于在node.js套接字服务器脚本中运行php代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆