如何使用node.js成功实现一个aws实例的http POST?

我正在设置一个aws实例来侦听HTTP POST。 当作为本地主机运行服务器和客户端时,一切似乎工作正常。 然而,当运行试图与客户端发送一个aws实例与服务器运行,我得到一个connect ECONNREFUSED错误。

我正在使用的aws实例(ubuntu服务器)具有对所有ip地址开放的端口80和8080。 我正在使用pm2模块来保持服务器运行。 尽pipe使用pm2给出了相同的错误。

服务器设置:( aws实例terminal)

 $ sudo apt-get install git $ curl -sL https://deb.nodesource.com/setup | sudo bash - $ sudo apt-get install -y nodejs $ sudo npm install pm2 -g --unsafe-perm 

使用节点启动服务器:

 $ node nodeServerTest.js 

使用pm2启动服务器:

 $ pm2 start nodeServerTest.js --name "nodeServerTest" -i max 

服务器代码:

 // nodeServerTest.js var http = require("http"); function startServer(port, ip) { // requestListener handles incoming requests function requestListener(request, response) { if (request.method == 'POST') { var body = ''; request.on('data', function (data) { body += data; // destroys connection if too long if (body.length > 1e6) { request.connection.destroy(); } }); request.on('end', function() { // checks for json, otherwise destroys connection try { var POST = JSON.parse(body); console.log('valid post:') console.log(POST) response.end('post accepted'); } catch(err) { console.log('bad post, ending connection') response.connection.destroy(); } }); } else { response.end(); } }; // creates the eventEmitter var server = http.createServer(requestListener); // beigns listening on specified port server.listen(port, ip); // logs when someone attempts to connect server.on('connection', function (stream) { console.log('someone connected!'); }); // notifies when server has started console.log('Server running at http://'+ip+':'+port); } startServer(8080, '127.0.0.1'); 

客户端代码:(当我尝试发布到aws实例时,我将主机字段更改为ip地址)

 // nodeClientTest.js function httpPost() { var http = require("http"); var postData = JSON.stringify({ 'msg': 'Hello World!' }); var options = { host: "localhost", // using correct public ip port: 8080, method: 'POST', path: '/', headers: { 'Content-Type': 'application/json', 'Content-Length': postData.length } }; var req = http.request(options, function(res) { console.log('STATUS: ' + res.statusCode); console.log('HEADERS: ' + JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', function (chunk) { console.log('BODY: ' + chunk); }); }); req.on('error', function(e) { console.log('problem with request: ' + e.message); }); // write data to request body req.write(postData); req.end(); } httpPost(); 

感谢您的帮助,如果需要更多信息,请留下评论

你在这里绑定你的服务器到loopback接口:

 startServer(8080, '127.0.0.1'); 

..只能在本地使用。 为了允许通过你的eth0接口进行访问,所以从其他networking主机取代这一行:

 startServer(8080); 

.. 根据Node.js手册在所有接口上监听端口8080 TCP。

PS请记住您的私有IP在AWS EC2!=公共IP地址上。 您可以通过在EC2主机上运行ec2metadata命令来检查两者。

根据OP要求的解释:

  • 每个TCP服务器套接字必须绑定到特定的networking接口。 这是因为你使你连接你的客户端TCP套接字到IP:port的组合并且IP被绑定到接口。
  • 具有127.0.0.1 IP地址的回环接口是仅作为来自本地主机的特定虚拟接口,针对ifself(因此名称),

所以,当你运行你的服务器绑定它回环的唯一方法,你可能只能提出请求,将启动从该主机本身的连接,例如像这样的telnet 127.0.0.1 8080telnet 127.0.0.1 8080

您可以将服务器绑定到eth0接口的实际IP上,但这是不实际的,特别是在私有IP发生更改的EC2服务器上。

这就是为什么我提出了更简单,通用的语法。 一个副作用就是这样你的服务器同时在loopback和eth0上进行侦听,但是这只对你有帮助,例如当你想根据所使用的接口把你自己的本地stream量和剩下的stream量分开时。

看起来你的服务器只绑定到服务器的本地地址(127.0.0.1),而不是监听服务器之外的连接。 请尝试将服务器的监听地址更改为0.0.0.0

 startServer(8080, '0.0.0.0'); 

我也build议你检查你的服务器的快速网页框架。