简体   繁体   English

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

[英]Running php code inside a node.js socket server script

I am running a normal TCP socket in javascript and uses node to execute. 我在javascript中运行一个普通的TCP套接字并使用node来执行。 Before I send a response back to the client, I want to validate the data with some some php (mysql) code. 在我将响应发送回客户端之前,我想用一些php(mysql)代码验证数据。 How can I execute this code inside this javascript file? 如何在这个javascript文件中执行此代码? All similar questions in STACKOVERFLOW asks about this code in HTML. STACKOVERFLOW中的所有类似问题都在HTML中询问此代码。 (which is okay and I understand that part), But this is a normal javascript file (executed in node). (这是好的,我理解那部分),但这是一个普通的javascript文件(在节点中执行)。 The javascript code is below and I've marked the validation function call. javascript代码在下面,我已经标记了验证函数调用。

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 : validateScanTag执行一个行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? 如何从javascript文件中调用此“geteriod.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: 我很确定问题本身值得重新思考(例如,将你的mysql代码从php移植到节点)但这是我的解决方案:

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 validator.php应该从stdin读取并写入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 . 另一个选择是使用fastcgi协议来通信节点< - > php或dnode rpc Or just use http and run php from nginx/apache (or even node-php ) 或者只使用http并从nginx / apache运行php(甚至是node-php

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. 据我所知,你试图使用node.js服务器调用的http post请求来运行你的php脚本。

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". 您可能首先必须意识到运行服务器端JavaScript通常具有不同的动机和可能性以及对您的体系结构的影响,而不是在浏览器中运行JS - 至少在经典的“jQuery意义”中。

As stated in your comment you expect jQuery to work seamlessly within server-side node.js. 正如您的评论中所述,您希望jQuery在服务器端node.js中无缝工作。 That doesn't work as the comment-posted ReferenceError: $ is not defined shows you. 这不适用于评论发布的ReferenceError: $ is not defined显示。 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). 你的node.js堆栈中没有可用的服务器端jQuery开箱即用(使用jQuery服务器端的可能性应该单独讨论,是另一个主题)。

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 . 然而,您显然正在寻找一种方法来重用PHP脚本,该脚本已经通过利用服务器端来处理您的mysql数据连接和处理。 Doing this using http there are possibilites like shown in this stackoverflow post or in the http.request section of the node.js documentation . 使用http执行此操作可能会显示在此stackoverflow帖子node.js文档的http.request部分中 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. 如果您的php脚本与node.js运行时位于同一服务器(或更好的运行时环境)上,您也可以通过使用php standalone(命令行)实用程序将其作为子进程生成, 直接执行php脚本

Last but not least: Yes, there are some ways (npm modules, ORM layers) for connecting mysql directly with your node.js app. 最后但同样重要的是:是的,有一些方法(npm模块,ORM层)可以直接用你的node.js应用程序连接mysql。 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. 我希望这有助于避免混淆不应该混淆的事情。

有一个PHP扩展在PHP中嵌入了Chrome的V8引擎

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM