Re: [nodejs] Two server: PHP Server send a GET request to a NodeJS Server

75 views
Skip to first unread message

Ryan Schmidt

unread,
May 17, 2013, 5:35:01 PM5/17/13
to nod...@googlegroups.com
On May 17, 2013, at 07:31, Dandi wrote:

> I'm going to develop this service: I use CakePHP Framework to develop a web application (consider this address for it: http://news.webapp.it) that allow clients to request news from social networks.
[snip]
> So, I think that a solution for my problem is to adopt a NodeJS server (that I can test at this url http://news.webapp.it/nodejs - because I made a folder in CakePHP root, named Nodejs, and customize .htaccess file for this) to make those several request for parsing pages, leaving free the direct channel with the server for other possible requests from client.

If this is what you want -- for CakePHP to deal with most of the site, and for nodejs to be used for just one part of the URL space on that same host and port -- then you'll have to configure your web server (Apache? Nginx?) to *proxy* requests for those URLs to nodejs. You don't need any "nodejs" folder in CakePHP because CakePHP has nothing to do with what the web server does to proxy those requests.

The only reason to go to all that trouble is if you need to publish any nodejs-specific URLs and you want the URLs to remain pretty and look like they're all part of a single service. But if you only need to access nodejs through AJAX then it doesn't matter what the URLs look like.

A simpler configuration would be to leave CakePHP handling all of your URLs on your main hostname and port, and start the nodejs server on a separate port and/or IP address and talk to it that way. The rest of the code you show below is already doing this.


> $(document).ready(function() {
>
> $('.first').click( function(){
> $.getJSON("http://192.aaa.bbb.ccc:8077/&callback=?",

Here, for example, you show that you're wanting to directly access the nodejs server at its port 8077 (while CakePHP remains on port 80 presumably through Apache or Nginx or similar web server).

Also, there appears to be a typo in the URL. You probably meant "?callback=something".

> function(data){
> console.log('Done with success!');
> },
> function(jqXHR, textStatus, errorThrown) {
> console.log('error ' + textStatus + " " + errorThrown);
> });
> });
>
> });
>
> </script>
>
>
> NodeJS Server-Side: server.js
>
> var http = require('http');
> var querystring = require('querystring');
> var url = require('url');
> var sys = require('sys');
>
> http.createServer(function (req, res) {
> var pquery = querystring.parse(url.parse(req.url).query);
> sys.puts("PQUERY: "+pquery);
>
> var callback = (pquery.callback ? pquery.callback : '');
> sys.puts("Callback: "+ callback);
> var returnObject = {message: "Hello World!"};
>
> var returnObjectString = JSON.stringify(returnObject);
>
> res.writeHead(200, {'Content-Type': 'text/plain'});
> res.end(callback + '(\'' + returnObjectString + '\')');
> }).listen(8077, '192.aaa.bbb.ccc');

It looks to me like this should work: it should create an http server on port 8077 responding to the hostname 192.aaa.bbb.ccc and delivering the "Hello World!" response to any requested URL. It may be simpler if you omit the hostname; then it'll respond to any hostname on that port number.


> This javascript client side code doesn't work but the server starts correctly and if I try http://news.webapp.it/nodejs in the browser address field, it return the message "Hello World".

That should not be. That suggests that the nodejs server is in fact running on port 80. Unless you've already configured some sort of proxy from your primary web server, but your message made no mention of that.



Dandi

unread,
May 18, 2013, 5:39:45 AM5/18/13
to nod...@googlegroups.com
Thanks for your help. So, is the solution only a right configuration of proxy on my server?
Can you explain in better way what I do?

Il giorno venerdì 17 maggio 2013 14:31:41 UTC+2, Dandi ha scritto:











Hi everyone,

I'm going to develop this service: I use CakePHP Framework to develop a web application (consider this address for it: http://news.webapp.it) that allow clients to request news from social networks. I used social API (Facebook, twitter, Google+) to send a request to know what client wants. It's simple and runs correctly. Each tweet or fb post that I receive can have a link and I use it to request a parsing of the webpage linked, making an AJAX request for each link to my server that will do CURL connection towards the external websites (I use readability lib). 
The problem is in this step: once loaded tweets/posts, I send several ajax request to my server, denying any other action that involves communication with server (Ajax again, for example - new requests go obviously into a queue). 
So, I think that a solution for my problem is to adopt a NodeJS server (that I can test at this url http://news.webapp.it/nodejs - because I made a folder in CakePHP root, named Nodejs, and customize .htaccess file for this) to make those several request for parsing pages, leaving free the direct channel with the server for other possible requests from client. But I cannot find a solution to do a communication between a CakePHP View script (Javascript) and NodeJS server. I tried this

CakePHP Client-Side : index.ctp

<input type="button" value="Get AUTH" class="first" />
<p class="codeAuth">CODE for auth: </p>
<p class="result"></p>

<script>
 
$(document).ready(function() {

$('.first').click( function(){
function(data){
  console.log('Done with success!');
},
function(jqXHR, textStatus, errorThrown) {
    console.log('error ' + textStatus + " " + errorThrown);
});
});
});

</script>


NodeJS Server-Side: server.js

var http = require('http');
var querystring = require('querystring');
var url = require('url');
var sys = require('sys');

http.createServer(function (req, res) {
    var pquery = querystring.parse(url.parse(req.url).query);   
    sys.puts("PQUERY: "+pquery);

    var callback = (pquery.callback ? pquery.callback : '');
    sys.puts("Callback: "+ callback);
    var returnObject = {message: "Hello World!"};
    
    var returnObjectString = JSON.stringify(returnObject);

    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end(callback + '(\'' + returnObjectString + '\')');
}).listen(8077, '192.aaa.bbb.ccc');

sys.puts("Server running at http://192.aaa.bbb.ccc");

This javascript client side code doesn't work but the server starts correctly and if I try http://news.webapp.it/nodejs in the browser address field, it return the message "Hello World".
I hope that I have explained my problem. 
Is there anyone that can I help me?

Ryan Schmidt

unread,
May 18, 2013, 5:30:31 PM5/18/13
to nod...@googlegroups.com
On May 18, 2013, at 04:39, Dandi wrote:

> Thanks for your help. So, is the solution only a right configuration of proxy on my server?
> Can you explain in better way what I do?

First get it working correctly with nodejs on a separate port (like 8077 like you're already doing in the code you showed). Later, you can decide whether it's worth the effort to set up a proxy from your main web server.

Reply all
Reply to author
Forward
0 new messages