Home > Enterprise >  How to connect a remote server from client by http.get of Node.js?
How to connect a remote server from client by http.get of Node.js?

Time:08-29

I'm using node.js to create connect to server on EC2.

The client.js is on the physical machine using a function http.get to get to the server.js on EC2, which using the express.get to connect.

When I ran the two file on the EC2 and my physical machine, nothing happened. And I had the error of Error: connect ETIMEDOUT.

Am I using the wrong ip address? What I use is the public Ipv4 ip.

server.js on EC2 with Ipv4 add: 100.27.18.131

const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
 calc = 0
 for (i=0;i<10000;i  ) {
 calc  = Math.random() * Math.random();
 }
 console.log(calc);
 res.send(calc.toFixed(10));
});
app.listen(port, () => {
 console.log(`listening on port ${port}`);
});

client.js on pyhsical machine:

const http = require('http');
setInterval(loadtest, 100); //time is in ms
function loadtest()
{
 http.get('http://100.27.18.131:3000', (res) => {
 res.on('data', function (chunk) {
 console.log(''   chunk);
});
 });
}

Error:

node:events:505
      throw er; // Unhandled 'error' event
      ^

Error: connect ETIMEDOUT 100.27.18.131:3000
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16)
Emitted 'error' event on ClientRequest instance at:
    at Socket.socketErrorListener (node:_http_client:454:9)
    at Socket.emit (node:events:527:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -4039,
  code: 'ETIMEDOUT',
  syscall: 'connect',
  address: '100.27.18.131',
  port: 3000
}

CodePudding user response:

Did not set up the port for 3000 or 80 at the vary beginning.

  • Related