The first thing out of every new grad’s mouth is ‘OMG! I need to start learning this language, make a new application, learn every data structure and algo in the book…’ and the list goes on. So yes, you guessed right after graduating from Flatiron’s software engineering program, I decided to take on Node.js!
Let’s now create our first Node.js Script! As you might have thought Node.js has a little something to do with the node module system and one of the most popular Node.js modules is the file system module where it allows a developer to communicate with files on any machine.
First thing is first, we need to import the module. The Node.js file system module is cleverly represented by the fs, so by requiring and naming a variable (which could be named anything but in the above example is ‘const fs’), you now have access to the file system module. This file system module is god’s gift to Node.js! But in the above example we are simply using the writeFile. The writeFile function takes three arguments: a filename, the data that you want stored in the file, and of course a callback function. Our callback function takes an error argument as well (if for some sad reason a problem may arise). Now to execute this Node.js script, just simply open a terminal and run ‘node filename.js’ and that’s it folks! Simply and easy enough!
Since we have a little intro to executing a Node.js script — below is a lil snippet of how beautiful and easy it is to implement Node.js as your server-side language…
Connect with me on linkedin and let’s be friends, Happy coding fam!