最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Two node.js servers? - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question asked Sep 19, 2012 at 8:46 AlexAlex 812 silver badges9 bronze badges 4
  • 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 Commented Sep 19, 2012 at 8: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 Commented Sep 19, 2012 at 10:12
  • ok so there it is AppFog doesn't allow ining connection from these ports – weakwire Commented Sep 19, 2012 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 Commented Sep 19, 2012 at 10:51
Add a ment  | 

3 Answers 3

Reset to default 4

On AppFog, you can see some documentation for running Node apps here: http://docs.appfog./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.

发布评论

评论列表(0)

  1. 暂无评论