0

I've a ues case, where I need to connect to the remote socket and send incoming HTTP payload. Here is a sample code that I've written,

const net = require('net');
const express = require('express');
var backendConfig = require('./backend_config')
const app = express();
const port = 8080;

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.post('/', function(req, res){
     const backendName = "UBPS";
     var client = new net.Socket();

     client.connect(backendConfig[backendName]["port"], backendConfig[backendName]["url"], function() {
     console.log('Connected');
     var a = Buffer.from(req.body.isoMessage, 'hex');
     console.log(a)
     client.write(a);
});

client.on('data', function(data) {
    console.log('Received: ' + data);
    res.send(data);
    client.end(); // kill client after server's response
});

client.on('close', function() {
    console.log('Connection closed');
});
});


app.listen(port, function () {
    console.log('Example app listening on port 8080!');
 });

When I curl to the endpoint, I get a message that the socket is connected and data is written however, I don't receive a message of response received. One more point, the standalone nodejs code works just fine.

var client = new net.Socket();
client.connect(port, ip, function() {
    console.log('Connected');
    var a = Buffer.from("245463", 'hex');
    console.log(a)
    client.write(a);
});

client.on('data', function(data) {
     console.log('Received: ' + data);
     var b = Buffer.from(data,"str");
     console.log(b.toString());
     var a = Buffer.from(data, 'hex');
     console.log(a.toString());
     client.end(); // kill client after server's response
});

client.on('close', function() {
     console.log('Connection closed');
});

Can someone help me understand where I'm doing wrong?

I'm ok with any other solution as well. Essentially, I want to connect to a socket server from node server. No restriction on the libraries.

Aashish P
  • 1,894
  • 5
  • 22
  • 36

1 Answers1

0

You have to put your code:

var client = new net.Socket();
client.connect(port, ip, function() {
    console.log('Connected');
    var a = Buffer.from("245463", 'hex');
    console.log(a)
    client.write(a);

outside of the app.post('/', function(req, res){ ... route.

Otherwise, the connection will only be opened when and only when that route is hit. Furthermore, because the new net socket instance is created under the post route, it will be limited as a block scope variables and this not accessible from the other routes.

Algo7
  • 2,122
  • 1
  • 8
  • 19
  • Thank you for your answer. The intention of creating new socket in the post routine is what you rightly mentioned. I want to create and kill the socket in the post routine. My problem is, somehow server is not responding or my callback function is not invoked. I don't receive any response at the client side. – Aashish P Apr 23 '20 at 10:44
  • You're welcome. In this case, we need to have a look at the client-side code as well – Algo7 Apr 23 '20 at 11:31
  • By the client-side, I mean the curl command nothing else. I think in the current code, the problem is with the asynchronous behavior of the net.Socket. – Aashish P Apr 23 '20 at 13:37
  • Yes. But I think it's better that we also see the front-end code too so we can know how they can connect in a better way. – Algo7 Apr 23 '20 at 13:54
  • I'm not sure what purpose will it solve. See, here Node service acting like a Socket proxy. Accepting HTTP request and sending it to a socket server. This is irrespective of how front end uses it. – Aashish P Apr 23 '20 at 14:03
  • You didn't make it clear the node server, in this case, is only a proxy instead of the actual backend. – Algo7 Apr 23 '20 at 19:00