简体   繁体   中英

Getting PHP from node.js

I am working on using node.js's connection abilities to continiously long poll a php script and I was wondering if anyone knows the thoery behind (maybe even a code sample) linking to php in node.js? I was thinking I need a new client and I add a request for the php page and then I add a response event listener which then fires off a event function which grabs the returned data and throws it back into the server function.

I am, however, a noob and need some initial guidance since their api documentation is not the easiest to read; the English is too wordy and it's white font on a dark background...not nice.

Thanks,

var sys = require('sys'),
   http = require('http'),
   url = require("url"),
   path = require("path"),
   events = require("events");

var twitter_client = http.createClient(80, "192.168.23.128");

var tweet_emitter = new events.EventEmitter();

function get_tweets() {
var request = twitter_client.request("GET", "/?url=ajax/session", {"host": "192.168.23.128"});

request.addListener("response", function(response) {
    var body = "";
    response.addListener("data", function(data) {
        body += data;
    });

    response.addListener("end", function() {
        sys.puts(body);
        var tweets = JSON.parse(body);
        if(tweets.length > 0) {
            tweet_emitter.emit("tweets", tweets);
        }
    });
});

request.end();
}

setInterval(get_tweets, 5000);


http.createServer(function (req, res) {
sys.puts("accessed Server");

res.writeHead(200, {'Content-Type': 'text/plain', "Access-Control-Allow-Origin": "*"});

var t = JSON.stringify({id:"test"});

var listener = tweet_emitter.addListener("tweets", function(tweets) {
    res.write(tweets);
});

 res.write(t);
  res.end();
}).listen(8124);
sys.puts('Server running at http://127.0.0.1:8124/'); 

This seemed to work. Taken from a mixture of other tutorials

Was just doing some research on this topic, and wanted to drop in an answer for anyone that might be looking to do the same thing.

The comments on the OP made good points as to whether or not this sort of thing would be an efficient use of resources, or a waste of nodes event-based processing abilities. I would say that passing requests on to an Apache/PHP server would be inefficient, because you're essentially doing the same thing as having recurring AJAX request sent to the Apache server. The only difference is you now have a man-in-the-middle sending the requests.

Apache is still serving requests just the same as it always is, it is just serving them to the Node.js server rather than the client. This does not build in any efficiencies, other than taking a bit of load off the client and placing it on the server.

The correct way to do this, as @Paul mentioned, is to use some sort of PHP processor that will allow you to bypass Apache. There's some fancy methods for getting this done using FastCGI and PHP-FPM - they're pretty high level so you might have some trouble integrating them into Node.js on your own.

On the bright side, there's a node module already being built to do just this: node-php . It's pretty young ("omega-alpha-super-beta-proof-of-concept"), but may be able to handle what you're trying to do. If it can't, at least it's a good starting point, and you can fork off to make your own additions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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