The Note Taker application can be used to write, save, and retrieve notes. The application uses the Express package for the back end framework. It will save and retrieve note data from a JSON file.
Deployed App on Heroku
Link to GIF of Application
https://drive.google.com/file/d/1-fN70Da55qfgyvn8uq_jFv9tV8EVjPYc/view
Github Repository
- Getting-Started
- Installation
- Test-Instructions
- Technologies-Used
- Contribution-Guidelines
- Cloning-Guidelines
- Screenshots
- GIF-of-Application
- Code-Snippets
- Learning-Points
- Authors
- License-and-Acknowledgements
- Contact
To begin the application, use the following in command line:
node server.js
- You will need to install Node.js. Here is a link below:
- Once you have downloaded Node.js, you will want to download node package manager (npm). In command line, you can enter:
npm install -g npm
- After installing npm, you have to initialize npm. In command line, you can enter:
npm init -y
- Now you have to install Express.js which is one of the many node packages. In command line, you can enter:
npm install express
To test the API, I recommend downloading Insomnia's API Platform and enter the following in Insomnia's URL:
Example POST body:
{
"title":"Notes Title",
"text":"notes text content"
}
*id is automatically generated so you do not need to enter id
Example DELETE: The API request below will delete note with id = "1"
To contribute, please follow these steps:
- Fork this repository.
- Create a branch:
git checkout -b <branch_name>
. - Make your changes and commit them:
git commit -m '<commit_message>'
- Push to the original branch:
git push origin <project_name>/<location>
- Create the pull request.
Alternatively see the GitHub documentation on creating a pull request.
To install this code, please use Github's guidlines to clone the repository
Github repository:
Screenshot of Homepage
Screenshot of Notes page
Link to GIF of Application
https://drive.google.com/file/d/1-fN70Da55qfgyvn8uq_jFv9tV8EVjPYc/view
This code snippet shows how you can create a delete route to delete an object with JavaScript, Node.js, and npm Express
-
"app.delete("/api/notes/:id", function (req, res)" will create a DELETE route to delete a note depending on id parameter. For example, if user enters "http://localhost:3000/api/notes/4", then it will delete the note with id: 4
-
"fs.readFile("db/db.json", "utf8", (err, data)" is used to read existing array data in db.json
-
"let savedNotes = JSON.parse(data);" is used because the data in db.json was stored as stringified array so we need to do a JSON.parse
-
"let noteId = req.params.id;" takes the id parameter entered by user which will later be used to recreate the notes object minus the note with the id parameter entered. For example, if user enters "http://localhost:3000/api/notes/2", then it will recreate the savedNotes array minus the object that has id: 2
-
savedNotes uses the .filter() method which creates a new array filled with elements that pass the test of notDeletedNotes.id is not equal to noteId
-
The for of statement loops through the values of an iterable object so it will loop through all of the notDeletedNotes in savedNotes array
app.delete("/api/notes/:id", function (req, res) {
console.log("notes delete route successful");
fs.readFile("db/db.json", "utf8", (err, data) => {
err ? console.error(err) : console.log(data);
if (err) throw err;
let savedNotes = JSON.parse(data);
let noteId = req.params.id;
savedNotes = savedNotes.filter((notDeletedNote) => {
return notDeletedNote.id != noteId;
});
let newNotesId = 0;
for (notDeletedNote of savedNotes) {
notDeletedNote.id = newNotesId.toString();
newNotesId++;
}
fs.writeFileSync(
"./db/db.json",
JSON.stringify(savedNotes, null, 2),
"utf8",
(err, data) => {
err ? console.error(err) : console.log(data);
if (err) throw err;
}
);
res.json(savedNotes);
});
});
-
How to use npm Express for routing URLs
-
How to use regular expressions for routing URLs
-
How to read and sync files with fs.readFile() and fs.writeFileSync() methods
-
How to use Insomnia's API platform to test API requests
1. Elliot Park
https://github.com/elliotpark410
https://www.linkedin.com/in/elliot-park/
This project is licensed under the MIT license via UC Berkeley's Extension Program
Big acknowledgements and thank you to Jerome Chenette, Manuel Nunes, Vince Lee, and Hannah Folk for their support and guidance!
If you'd like to learn more about my projects, check out my Github profile: https://github.com/elliotpark410
If you have any questions, please don't hesitate to email me at elliotpark410@gmail.com
Copyright (c) 2022 Elliot Park