我想在不同的端口上运行两个 node.js httpservers:
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);
我想制作一个 android 应用程序,它将连接到我在 AppFog 主机上的 8888 端口上的 Node.js 服务器,向服务器发送消息,并从它接收响应。如果我从浏览器打开我的服务器,我只会得到一个简单的 html 页面。但是我的代码不起作用。为什么?
最佳答案
在 AppFog 上,您可以在此处查看有关运行 Node 应用程序的一些文档:http://docs.appfog.com/frameworks/node
一个重要的部分是如何确定绑定(bind)到哪个端口。在您的代码示例中,您有 s.listen(8080);但要指定的端口实际上在环境变量中:
s.listen(process.env.VCAP_APP_PORT || 8080);
AppFog 目前不支持为同一个应用程序打开两个端口,因此您必须将其拆分为两个应用程序并将第二个应用程序类似地绑定(bind)到环境变量:
dbserver.listen(process.env.VCAP_APP_PORT || 8888);
AppFog 将在几个月内获得 WebSocket 支持,但目前尚未提供。
关于javascript - 两个 node.js 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12491251/