0

I'm trying to connect to a server/specific port using Node.js, and I don't even get past var net = require('net');

I'm using Node.js v16.15.0.

Welcome to Node.js v16.15.0.

When I use the command above, I receive UNDEFINED. As far as I know, I've installed everything I need (including socket.io), and I'm working within the Node.js environment in iTerm.

My goal is to connect to a TCP server, receive a list of files, and then download each of them over a persistent socket. But I'm a little stuck as I can't even seem to get into the TCP server in the first place.

This is what I think I'm supposed to run to get in (obviously with my correct port and IP info which is omitted below).

var HOST = 'IP';
var PORT = 'PORT'
var FILEPATH = 'myfilepathhereIwilltweakitwhenIgettothispoint';

Can anyone point me in the right direction?

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 02 '22 at 03:52

1 Answers1

0

From what you said, I think you are trying to code NodeJS script within the NodeJS executable start in command line. You get an UNDEFINED because you imported the library into your variable and this assignment does have any value, so it is UNDEFINED. You can read more about this in this subject : link

But what we usually do in NodeJS development is creating a file, let's call it index.js. Inside that file we are writing our code, let's say :

const net = require('net');
const client = net.createConnection({ port: 8124 }, () => {
  // 'connect' listener.
  console.log('connected to server!');
  client.write('world!\r\n');
});
client.on('data', (data) => {
  console.log(data.toString());
  client.end();
});
client.on('end', () => {
  console.log('disconnected from server');
});

Code sample from NodeJS Documentation.

Then we want to run our code by using the command line like this : node path/to/index.js.

Hope it helps !

Skylli
  • 116
  • 7