Learning NodeJs by creating a Notes app

Nabendu Biswas
Dev Genius
Published in
9 min readDec 28, 2021

--

We will start learning about NodeJS from the beginning of this series and will jump directly into the coding part. What and why use NodeJS a lot of us know and skip those details here.

You can find this in video format on my YouTube channel.

We will first learn about the Node.js Module system. First, create a folder simple-app and open it in VS Code. Create a file server.js in it and add the below content in it.

Here, we are first importing the in-built fs module and then using it with writeFileSync() to create a file notepad.txt and add the given text in it.

server.js

Now, we will run the file from the terminal with node server.js command

node server.js

And it will create a notepad.txt file in the same directory with our content.

notepad.txt

But if we run the same command with updated value like below in server.js file, it will replace the content in notepad.txt file.

server.js

Now, if we want to append data to a file and not update the existing data, we use the appendFileSync() method.

server.js

Now, after running node server.js, we will get the updated content added in notepad.txt.

node server.js

Next, we will learn to use one file details in another file. Create an app.js file and add the below content to it.

Here, we are importing the server.js file and using it in writeAndappend variable. Next, we are calling the writeAndappend().

app.js

Now, we will update or server.js file to have a function writeAndappend(), which call the writeFileSync and appendFileSync function. We are also exporting it.

server.js

Next, run the app.js from the command line and we will get the console output.

Console

We are also getting the proper text added in notepad.txt file.

notepad.txt

Now, we use a lot of third party npm packages in our node application. And to use these packages we need package.json file. For that use the below command.

npm init -y

It will take all the default values and create a package.json file.

package.json

Now, we will add our first package which is validator and it is used to validate strings. We have to install it with the below command in which we are also mentioning the version.

npm i validator@10

Now, in our app we can see a node_modules folder been created and the dependency of validator been added in package.json file.

package.json

We can now use it in our app.js file. Here, we are first importing the validator and after that we are using one of the validator method isEmail, we check whether the passed string is an email.

app.js

Now, when we will run node app.js from command line, we will see true for the first command and false for the second one.

node app.js

Next, we will add a very popular package of nodemon in our project. This package continuously run a process, which checks for any changes in our project and it automatically gets reflected. So, add it by the below command.

npm i nodemon

Also, update the package.json file to change the start command.

package.json

Now, in the terminal run the command npm start it will check for any changes in app.js file.

npm start

Now, if we want to get value from the user, through the terminal. We get it through process.argv function. Add the below line in app.js file.

app.js

Now, on running node app.js Nabendu from terminal, we get an array in which the third item is Nabendu.

node app.js Nabendu

Now, we will be creating a small Note app, so create a new file index.js and inside it add the below content. Here, we are checking the second item of the array and showing different console in it.

index.js

Now, if we run the index.js file with new or delete as a second parameter, we will get the respective output.

index.js

Now, we will update our index.js file to take the title and body arguments also. Now, they will be a third and fourth item. We are also passing the data from these two in a new function called newNote() in notes.js file.

index.js

Now, create a new file notes.js and add the below content in it. We are just receiving the title and body from here and console loggin it.

notes.js

Now, when we run the node index.js command with arguments, we will get the appropriate console logs.

node index.js

Now, we will add the functionality in notes.js to add the title and body in a notepad.json file.

Here, we are calling a function uploadNotes() from newNote function. Inside the uploadNotes function, we are first reading a file notepad.json. After that converting to a string and returning it. If the file is not available then return an empty array.

Back in newNote, we are adding the title and body in the notes array. After that, we are writing to notepad.json using writeFileSync() and passing the array of object after stringifying it.

notes.js

Now, run the node index.js command with two different title and body from command line.

node index.js

Now, a new notepad.json file will be created with our two objects in an array.

notepad.json

Now, we will add the functionality to delete a note, by passing the title from the command line.

In index.js add the code to take the third argument and then call a function deleteNote.

index.js

Now, in notes.js file we will add the deleteNote function. This function is simply filtering out the matched title and writing back to notepad.json file.

notes.js

Now, from the terminal give the below command and mention the title to delete.

node index.js

Now, in the notepad.json the title will be removed.

notepad.json

Now, we will add the simple functionality to list all notes title. Here, again in index.js we will first create look for a new command list. After that we will call a function allNotes().

index.js

Next, in our notes.js file we will create the allNotes function. Here, we are getting the array in notes. After that we are looping through the notes and console logging each title. Notice that we are using arrow functions here, which is completely valid.

notes.js

Now, in the terminal if we give the command node index.js list, we will get the title of all the notes.

node index.js list

Lastly, we will create the logic to find a note and display it’s content, if the note is found. So, in index.js add a new else if statement.

index.js

Now, we will write the findNote function in notes.js file. The logic for this is also very simple and we are just finding the note from the title and displaying it.

notes.js

Now, from the command line we can find a note with an existing title or with no title.

Finding note

We will now learn to add debugger in our code and use the debugging tool available in chrome. First add the debugger at the point, where you want to debug the code in notes.js file.

notes.js

Then run the node command, but with inspect added in it.

inspect

Next, open chrome://inspect in chrome browser to debug our node application. If everything is right, you will see the Target with inspect. Click on the inspect link.

inspect

It will open the Dev tools and you need to click on +Add folder to workspace button.

Add folder

Now, we need to select the folder containing our app. It will also ask us to allow, which we will do.

Adding folder

Now, we will see the debugger been paused and can click on the Run button.

Run

On running, we will see the data at the debugger point in various format and also can see it in console. One shortcut to open this console is by the Esc key.

data

Pressing the run button again, will bring us out of debugger and print the console logs.

Console

We can start the debugger again from the console by running the command restart and can come out of it by pressing the Ctrl+C twice.

Debugger

This completes our small notes app with Node.js in which we have also learned about debugging. The code for the same is in this github repo.

--

--