![]() |
Building a note-taking application using Node.js involves several steps, from setting up the server to managing the application logic and user interface. This guide will walk you through the process of creating a simple note-taking application using Node.js, Express, and a few other key technologies. FeaturesWe will create a basic note-taking application where users can:
ApproachWe are going to use Body Parser by which we can capture user input values from the form such as Notes’ Content, and Notes’ Id, and store them in a collection. Then we are going to send the Notes to the web page using EJS. EJS is a middleware that makes it easy to send data from your server file (app.js or server.js) to a web page. We are also going to create the Delete Route for deleting notes, and an Update Route for updating the notes. Steps to Setup Project to Build NoteStep 1: Create Project DirectoryMake a new folder with project name and navigate to it using below commands mkdir myapp Step 2: Initialize React ProjectInitialize the NodeJs project inside the myapp folder. npm init -y Step 3: Install required ModulesInstall the necessary packages/libraries in your project using the following commands. npm install express ejs body-parser Project Structure:![]() The updated dependencies in package.json file will look like: "dependencies": { Step 4: Create Server FileCreate an ‘app.js’ file, inside this file require the Express Module, and create a constant ‘app’ for creating an instance of the express module, then set the EJS as the default view engine. const express = require('express'); Step 5: Rearrange Your DirectoriesIt is required to use ‘.ejs’ as an extension for the HTML file instead of ‘.html’ for using EJS inside it. Then you have to move every ‘.ejs’ file in the views directory inside your root directory. EJS is by default looking for ‘.ejs’ files inside the views folder. Step 6: Use EJS variableInside your updated .ejs file, you have to use EJS Variables to receive values from your server file. You can declare variables in EJS like <%= variableName %> Example: Implementation to write the code for building note taking application.
Step 7: Send data to a variableInside your server file ( app.js or index.js ), you can send an EJS file along with some data by using the render method. app.get("", (req, res) => { Example: Implementation to write the code for to create server.
Step 8: Require body-parser moduleconst bodyParser = require('body-parser') And then: app.use( bodyParser.json() ); Then we can handle form data using the request object. Step 9: Fetch NotesWe have an array of notes with two properties content and id. const notes = [{ Let’s send the array to our web page. In the previous step, we just sent a value to the variable, now we are sending the complete array. app.get("", function (req, res) { Step 10: Create FormSince it is common to have so many elements(notes) inside our array and we have to print each of them, we have to use For Each Loop to loop through every single element inside our collection and display the details in the text area field of the update form. We will display the details inside the text area of the update form, this way we can show the notes, and same time we can use it to update notes. <form action="/update" method="post"> Step 11: Update NotesNow, let’s see how we can handle this update form in our update route inside the server file(app.js). app.post('/update', (req, res) => { For updating a note, we have to create an updated route where we are going to fetch the requested note id and search for the element which has the same note id, and change the element content property to the ‘noteContent’ which we get from the update form. Step 12: Delete NotesUpdating Web Page giving a delete option: We have to create a form that sends the note id which we want to delete to the server file ‘app.js’. <form action="/delete" method="post"> For deleting a note, we have to create a delete route where we are going to fetch the requested note id and search for the element which has the same note id, and delete the element. app.post('/delete', (req, res) => { Example: Below is the complete code to build Note Taking Application using Node.js:
Output: |
Reffered: https://www.geeksforgeeks.org
Node.js |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |