Two node.js servers?

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?

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.

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!

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.