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.
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);
net.Connect(). Find it here: nodejs.org/api/…