How to create your very own file transfer program in node.js 2020

Hello there! So you probably know that in the year 2019, JavaScript has evolved so much especially since the advent of the node.js engine. Node.js allows Javascript to run in a native environment. Today we’ll be learning how to build a file transfer program using node.js

Incase you want to clone the code and follow along here is the github repository. Also the commands I used (mkdir, touch, etc) are linux based. You can replicate them on your own OS.

First of we’ll have to create a new folder to keep our code (obvi…), I’ll call mine terminect.

then navigate to the terminect directory and create two files: sender.js and receiver.js
Also create two new directories in terminect: sender and receiver

Now in sender.js put in the following code:

On line 1 we first import two node modules: the net module for TCP Connections and the fs module for file system manipulation functions. Now put in any file into the sender folder you created. (In the example our file is “SC-02.pdf”).
On line 3, we create a stream that reads from “SC-02.pdf” asynchronously and name it istream. Then we create a new TCP server called server on line 5.

A little note on streams

In node, streams are… well streams. They are structures that encourage the flow of data in buffers via pipes. In node.js, streams are asynchronous and we either read from a stream or write to a stream at a particular time (Except in duplex and transform streams).

In node.js, files, I/O and sockets are all streams and we’ll be using that to our advantage. So let’s continue with our code…

On line 7, we check to see if the file is readable by listening for the ‘readable’ event. when you open a R/W stream to a file, it emits a readable event as soon as it can be read (probably it was being used by another process beforehand). Then we use a callback to stream data to the socket object provided when we initialized the server (Note that this socket object is what clients connect to and read data from). So we are basically piping data from the file to the socket. Now this data is in the socket but since there is no outlet from the socket, the data remains in the socket until another stream pipes the data away from the socket. We close the server as soon as the socket connection ends (If a connection to the socket was terminated). We also close the socket once the read stream has ended or has been exhausted since there is no longer use for it.

Put in the following code in receiver.js:

We import the modules as usual and get down to business. on line 4, we connect to the TCP server we created at port 8000 (remote_server is an IP address if you are not running on same computer) and name socket retrieved socket. Then we create a stream that writes to a new file in receiver, “SC-02.pdf” (file will be created by stream) and call it ostream. The “data” event of the socket is emitted anytime data is written to it (which we did in sender.js). So we use the data passed in the callback and write it to the file using the write stream. Line 11 & 12 help log our information on the sender and receiver consoles (Line 11 pipes the data to the socket which in turns pipes it to the output of sender’s console). Finally we listened for the socket to be ended (after the read stream on sender.js has been emptied) and exit the process.

run node sender.js then open up another terminal and run node receiver.js

Okay folks! that’s it! I hope you were able to follow through will the tutorial.
As an added feature, the program works over a local network. Just run node sender.js on the sending computer. Then run node receiver.js [IP_ADDRESS] on the receiving computer where IP_ADDRESS is the IP address of the sending computer. Don’t forget to make sure that port 8000 is not in use.

📝 Read this story later in Journal.

👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.

I write about computers, servers, backend frameworks and every other thing you came here to see.