What is Node-js ? Build Notes App using node-js.

Prem Parmar
5 min readSep 13, 2020

The author of node.js is Ryan Dahl. Usually JavaScript is used to make interactive websites (front-end) but nodejs is used to make backend API services.

Nodejs is single threaded and used for non-blocking / low CPU intensive tasks. For frontend development, there is V8 engine for chrome which is used to run javascript code. It is same for backend as well, here in backend V8 engine is appended and JS code runs there.

In frontend, we use window object and In Node we use global object. We don’t use DOM in backend so in node we don’t have “document” object there. Node.js uses C++ bindings for working with File and other tasks.

Let us first download node in our local environment :-

  • Go to Node.js Official Website : NodeJS and Download LTS version (Long Term Support)
  • Open Terminal and type node
  • if it was downloaded successfully then you get REPL (Read–eval–print loop) aka Interactive Language Shell. It also shows which node version you are using.

Now its time to create Notes app using Nodejs. First of all we require good IDLE so we can code easily. I am using Visual Studio Code here but you can use any like Atom, Webstorm, Sublime-text etc.

You can download VSCode from here : Visual Studio Code

In Node, for importing any file or module we are using require method. module is built by some other developer which can make our life easy.

If you want to setup notes project in your vs code you can download it from github :

https://github.com/premparmar11/Notes-App.git

In Notes App project, We take command line arguments and use it to save in file. Here our database is simple JSON file. you can access command line arguments with process.argv.

Let take some simple script of node JS and run through node command:

You can see that process.argv prints List which contains path of node install, path of file and the argument passed >> node app.js prem in list-console.

Sometimes you have noticed that when you type some command then help/version it gives some data / description of that command. This can be done using “Yargs” module. We also use “Chalk” to make console.log text more colourful.

Let us make folder named notes-app and import it into VSCode. after that make file app.js. It is naming convention used in industries to make project more readable and understandable.

GO to terminal and run npm init command which asks for some details like package name / author name / version etc. and generate package.json file in project.

npm stands for node package manager. this command is available when you download node in your local environment. To install any other library / module you need just type “npm install lib-name”.

Firstly we download yargs and chalk library in our project :

  • npm install yargs
  • npm install chalk

Your VSCode will look like this now:

Folder structure of project will look like this. we will create command like npm help which gives some description about that command using YARGS. node_modules contains all the dependencies of project and you can add other dependencies manually in package.json’s dependencies key.

Let us start notes project by Adding note feature :

  • import module yargs in app.js file
  • yargs.command is function which takes object as argument and that object has four keys : command, handler, builder, describe
  • command : The name of command like “npm”, here its “Add”- Case Sensitive
  • describe : Feature provided by Add command
  • builder : arguments pass after Add command we can add as many as we want but here i only add one you can also add two. “title” as argument, demandOption makes this argument compulsory and type is type of argument you require to pass.
  • handler : It is used to handle arguments or function to perform anything.

Above code in terminal we need to pass like this:

handler contains function and we add note in notes.json file using other module called ‘fs’. It is inbuilt module so we don’t need to install it. Just need to import using require(‘fs).

This is notes.js file contains all the utility of adding/reading and removing notes.

fs.writeFileSync(“File Name.ext”, Data); used to write data in file and fs.readFileSync(“File Name.ext”); used to read data from that file if this file is not exist then it throws exception so we have put it into try and catch block.

Two important methods is too much important to know:

  • JSON.parse : It is used to convert from string JSON data to JSON Object.
  • JSON.stringify : It converts JSON object to string.

loadNotes() function loads title of notes and if it is duplicate then prints “Duplicate Note Found” in addNote() function else save in file.

Chalk

This is how chalk can print in console. We have used chalk.colorname.inverse(“message to print”).

We have also make Read, Write, Remove function like this you can download it from my github profile and understand it. If you find any query regarding this, then feel free to contact me.

That’s it for today. Be Ware, Be Safe and WEAR MASK 😷❗❗

--

--

Prem Parmar

Software Engineer, having 3 years of experience in Ecommerce / HCM domain Product based company.