3

I want to run two node.js httpservers on different ports:

var http = require('http');        

var dbserver = http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('<html><body><h2 align=center>TEST index.html.</h2></body></html>');
    res.end();
});

dbserver.listen(8888);

var s = http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write('hello world');
    res.end();
});

s.listen(8080);

I want to make android application that will connect to my Node.js server on port 8888 on AppFog hosting, send a message to the server, and receive a response from it. And if i open my server from browser i get just a simple html page. But my code doesn't works. Why?

Alex
  • 81
  • 2
  • 9
  • 2
    What is the error? if you telnet the servers you get a connection? Does AppFog allow connections to these ports? if (Yes,Yes) then you have to post your android code. – weakwire Sep 19 '12 at 08:50
  • Well, i think that the problem not in my Android App, it just have button and on click it creates a http client that opened a connection to the server site test.rs.af.cm on port 8888. At the AppFog site there are tools to Stop/Start/Restart server and if my server is stoped then it wouldn't start.. The hint "Starting" is on the screen and doesn't disappear. If i connecting to my server with browser on port 8080 it says "AppFog 404 Not Found", telnet says "..Cannot open a connection. Connection lost..." on port 8888, and Titanium says 'Sending error The target server failed to respond' – Alex Sep 19 '12 at 10:12
  • ok so there it is AppFog doesn't allow incoming connection from these ports – weakwire Sep 19 '12 at 10:14
  • i wrote a mail to AppFog support team, i will post their answer as well.. I asked here because i thought that maybe someone here have similar problems... – Alex Sep 19 '12 at 10:51

3 Answers3

4

On AppFog, you can see some documentation for running Node apps here: http://docs.appfog.com/frameworks/node

One important piece is how to determine which port to bind to. In your code sample, you have s.listen(8080); but the port to specify is actually in the env var:

s.listen(process.env.VCAP_APP_PORT || 8080);

AppFog does not currently support having two ports open for the same app, so you will have to split this into two apps and have the second one similarly bound to the env var:

dbserver.listen(process.env.VCAP_APP_PORT || 8888);

AppFog will have WebSocket support within a few months but it is not available today.

cardmagic
  • 241
  • 1
  • 3
2

Well, well, well, I've had an e-mail answer from AppFog support it sounds like this:

//----------------------------------------------------------------------

Joe, Sep 19 12:30 (PDT):

Hi!

Unfortunately, only HTTP traffic is supported on AppFog, so websockets and UDP traffic won't work. Websocket support is on our roadmap, though, so please stay tuned!

Joe AppFog Support

//---------------------------------------------------------------------

So the problemm was not in Node.js, and not in my code, but on AppFog. Thank you all very much for your help!

Alex
  • 81
  • 2
  • 9
1

You can certainly run two different servers on two different ports in a single node app, but if you want your client-side code to access both of them, you're very likely to run into same-origin-rule issues (e.g. a browser running code loaded from one server will not, in general, be able to do an AJAX request to the other server, since two different ports on the same URL are considered to be two different origins). Your cross-server connection ability is going to be limited to requesting scripts (which includes JSONP requests) and Websocket connections (though remember that if you're using socket.io and the client doesn't support Websockets, the fallback transport methods that socket.io uses won't necessarily work cross-origin.

ebohlman
  • 14,795
  • 5
  • 33
  • 35
  • I want to make an application on android that will get information from the server(node.js) side, that will connect to a database. But information in the database can be changed at the web-site, so my site doesn't need to send/recv information with the server on port 8888, and my app doesn't need to communicate with the server on port 8080. – Alex Sep 19 '12 at 10:17
  • Your comment isn't particularly clear: under what circumstances does you Android client code need to contact (one of your) server(s), and what is it supposed to do when it gets a response? – ebohlman Sep 19 '12 at 10:36
  • let's say that i'm making some sort of images gallery and description of the images stored on my server in "database", my android app shows me all that images by groups, so.. it need to send a request to the server and get those images and descriptions.. or it can store an images at the local storage on the device and get all the info from there. My another client side is HTML web-site, that allows me to upload new images and add information to them, in my "database". – Alex Sep 19 '12 at 10:50
  • OK, you've got three types of client/server interaction: 1) Client asks server for a list of images; server sends document listing them. 2) Client, upon receiving response from (1), asks server (probably several times) for an image. 3) Client captures image, gets annotation from user, and sends image+annotation to server for permanent storage. Think those requirements through, come up with some idea of how your app can handle them, and come back here if you're having trouble. – ebohlman Sep 19 '12 at 10:56
  • the problem was at AppFog hosting side. Thanks. – Alex Sep 20 '12 at 04:50