1

I am new to Node.js and I have 2 questions about it:

  • Can you create regular TCP sockets on the server's side?
  • Is it possible to read/write to a file on the server's side?

That's all. Both of these are critical for putting my program on the web.

0

1 Answer 1

2

Node has inbuilt modules that have the functionality you're looking for. You can create raw TCP sockets on the server side with the native net module.

var net = require('net');
net.createServer(function(socket) {
  socket.write('data');
  socket.end();
});

And there is also a fs module for file system manipulation:

var fs = require('fs');

var data = 'a string';
var file = './file';

fs.writeFile(file, data, function(err) {
  if (err) throw err;
  // file has been written to disk
});

// or synchronously writing a file
fs.writeFileSync(file, data);

// fetch the data asynchronously
fs.readFile(file, function(err, data) {
  // we have "a string"
});

// synchronously reading a file
var str = fs.readFileSync(file);
Sign up to request clarification or add additional context in comments.

5 Comments

I want to create a client-like socket that connects to a server, not a server-sided TCP socket.
You asked for a "regular TCP socket on the server's side". What are you asking for then? Connecting to a TCP server with Node?
The web application will act as kind of a 'relay' to another server. It will act as a client. So I need to create client sockets.
Just use net.Connect(). Find it here: nodejs.org/api/…
Okay, thank you. I will first try to get a program from a Socket.IO tutorial running, then completely modify the code so that I can test TCP sockets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.