How to Build Note Taking Application using Node.js?
Last Updated :
24 Jul, 2024
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.
Features
We will create a basic note-taking application where users can:
- Create Notes: Add new notes with a title and content.
- View Notes: See a list of existing notes.
- Edit Notes: Modify the content of existing notes.
- Delete Notes: Remove notes from the list.
Approach
We 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 Note
Step 1: Create Project Directory
Make a new folder with project name and navigate to it using below commands
mkdir myapp
cd myapp
Step 2: Initialize React Project
Initialize the NodeJs project inside the myapp folder.
npm init -y
Step 3:Â Install required Modules
Install 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": {
"body-parser": "^1.20.2",
"ejs": "^3.1.10",
"express": "^4.19.2",
}
Step 4: Create Server File
Create 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');
const app = express();
app.set('view engine', 'ejs');
Step 5: Rearrange Your Directories
It 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 variable
Inside 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.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<%= variableName %>
</body>
</html>
Step 7: Send data to a variable
Inside 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) => {
res.render("home",
{ variableName: "Hello Geeks!" })
})
Example: Implementation to write the code for to create server.
Node
// app.js
const express = require('express')
const app = express()
app.set('view engine', 'ejs')
app.get("/", (req, res) => {
res.render("home", { variableName: "Hello Geeks!" })
})
app.listen(3000, (req, res) => {
console.log("App is running on port 3000")
})
Step 8: Require body-parser module
const bodyParser = require('body-parser')
And then:
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({
extended: true
}));
Then we can handle form data using the request object.
Step 9: Fetch Notes
We have an array of notes with two properties content and id.
const notes = [{
noteId: 1,
noteContent: "Hey, Geeks you can add your important notes here."
}
]
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) {
res.render("home", {
data: notes
})
})
Step 10: Create Form
Since 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="/https/www.geeksforgeeks.org/update" method="post">
<input type="number" style="display: none;"
name="noteId" value="<%= element.noteId %>">
<textarea type="text" rows="6" cols="30"
placeholder="Type Here..." name="noteContent"
value="<%= element.noteContent %>">
<%= element.noteContent %></textarea>
<br>
<button type="submit">Update</button>
</form>
Step 11: Update Notes
Now, 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) => {
var noteId = req.body.noteId;
var noteContent = req.body.noteContent;
notes.forEach(note => {
if (note.noteId == noteId) {
note.noteContent = noteContent;
}
})
res.render("home", {
data: notes
})
})
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 Notes
Updating 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="/https/www.geeksforgeeks.org/delete" method="post">
<input type="number" style="display: none;"
name="noteId" value="<%= element.noteId %>">
<button type="submit"
style="margin: 0px 4px;">✕</button>
</form>
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) => {
let noteId = req.body.noteId;
var j = 0;
notes.forEach(note => {
j = j + 1;
if (note.noteId == noteId) {
notes.splice((j - 1), 1)
}
})
res.render("home", {
data: notes
})
})
Example: Below is the complete code to build Note Taking Application using Node.js:
HTML
<!-- home.ejs -->
<!DOCTYPE html>
<html>
<head>
<title>Note Keeper</title>
</head>
<body>
<h1>Note Keeper</h1>
<div style="display: flex;">
<% data.forEach(element=> { %>
<form action="/update" method="post">
<input type="number" style="display: none;"
name="noteId" value="<%= element.noteId %>">
<textarea type="text" rows="6" cols="30"
placeholder="Type Here..." name="noteContent"
value="<%= element.noteContent %>">
<%= element.noteContent %></textarea>
<br>
<button type="submit">Update</button>
</form>
<form action="/delete" method="post">
<input type="number" style="display: none;"
name="noteId" value="<%= element.noteId %>">
<button type="submit"
style="margin: 0px 4px;">✕</button>
</form>
<% }) %>
</div>
<h1>Add Note</h1>
<form action="/" method="post">
<input type="number" style="display: none;"
name="noteId">
<textarea type="text" rows="6" cols="30"
placeholder="Type Here..."
name="noteContent"></textarea>
<br>
<button type="submit">Add</button>
</form>
</body>
</html>
JavaScript
// app.js
const express = require('express')
const bodyParser = require('body-parser')
const notes = [{
noteId: 1,
noteContent: "Hey, Geeks you can add your important notes here."
}
]
const app = express()
app.set('view engine', 'ejs')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}))
app.get("/", function (req, res) {
res.render("home", {
data: notes
})
})
app.post("/", (req, res) => {
const noteContent = req.body.noteContent
const noteId = notes.length + 1;
notes.push({
noteId: noteId,
noteContent: noteContent
})
res.render("home", {
data: notes
})
})
app.post('/update', (req, res) => {
var noteId = req.body.noteId;
var noteContent = req.body.noteContent;
notes.forEach(note => {
if (note.noteId == noteId) {
note.noteContent = noteContent;
}
})
res.render("home", {
data: notes
})
})
app.post('/delete', (req, res) => {
var noteId = req.body.noteId;
var j = 0;
notes.forEach(note => {
j = j + 1;
if (note.noteId == noteId) {
notes.splice((j - 1), 1)
}
})
res.render("home", {
data: notes
})
})
app.listen(3000, (req, res) => {
console.log("App is running on port 3000")
})
Output:
Similar Reads
Unit Testing of Node.js Application
Node.js is a widely used javascript library based on Chrome's V8 JavaScript engine for developing server-side applications in web development. Unit Testing is a software testing method where individual units/components are tested in isolation. A unit can be described as the smallest testable part of
5 min read
How to Create Sentiment Analysis Application using Node.js ?
Sentiment Analysis is a natural language processing technique used to determine emotions from textual data. It's often performed to know customer requirements, study product sentiment in user feedback, decision-making, and more.Types of Sentiment Analysis:Fine-grained Sentiment Analysis: It is done
10 min read
Todo List Application using MEAN Stack
The todo list is very important tool to manage our tasks in this hectic schedule. This article explores how to build to-do list application using the MEAN stackâMongoDB, Express.js, Angular, and Node.js. Weâll walk you through the process of setting up backends with Node.js and Express.js, integrati
10 min read
Todo List CLI application using Node.js
CLI is a very powerful tool for developers. We will be learning how to create a simple Todo List application for command line. We have seen TodoList as a beginner project in web development and android development but a CLI app is something we don't often hear about.Pre-requisites:A recent version o
13 min read
Build an Application Like Google Docs Using React and MongoDB
In this article, we will create applications like Google Docs Made In React and MongoDB. This project basically implements functional components and manages the state accordingly. The user uses particular tools and format their document like Google Docs and save their work as well. The logic of Goog
6 min read
How do you debug Node applications?
Debugging is a crucial aspect of software development, including NodeJS applications. NodeJS offers several built-in debugging options and tools to help developers identify and fix issues efficiently. Let's explore some common techniques and tools for debugging NodeJS applications. Using console.log
3 min read
Freelancer Portfolio Builder Application using MERN Stack
In today's digital age, having a professional portfolio is essential for freelancers to showcase their skills and attract potential clients. In this article, we'll dive into the process of building a Freelancer Portfolio Builder using the MERN (MongoDB, Express.js, React.js, Node.js) stack. This pro
5 min read
How to build Node.js Blog API ?
In this article, we are going to create a blog API using Node.js. A Blog API is an API by which users can fetch blogs, write blogs to the server, delete blogs, and even filter blogs with various parameters.Functionalities:Fetch BlogsCreate BlogsDelete BlogsFilter BlogsApproach: In this project, we w
4 min read
How to read and write JSON file using Node ?
Node JS is a free and versatile runtime environment that allows the execution of JavaScript code outside of web browsers. It finds extensive usage in creating APIs and microservices, catering to the needs of both small startups and large enterprises.JSON(JavaScript Object Notation) is a simple and t
3 min read
Todo List Application using MERN
Todo list web application using MERN stack is a project that basically implements basic CRUD operation using MERN stack (MongoDB, Express JS, Node JS, React JS). The users can read, add, update, and delete their to-do list in the table using the web interface. The application gives a feature to the
8 min read