/Note-Taker

The Note Taker application can be used to write and save notes. It will use an Express.js back end and will save and retrieve note data from a JSON file

Primary LanguageJavaScriptMIT LicenseMIT

Note-Taker

License: MIT

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.


Links

Deployed App on Heroku

https://note-taker-elliotpark.herokuapp.com/


Link to GIF of Application

https://drive.google.com/file/d/1-fN70Da55qfgyvn8uq_jFv9tV8EVjPYc/view


Github Repository

https://github.com/elliotpark410/Note-Taker


Table of Contents


Getting-Started

To begin the application, use the following in command line:

node server.js

Installation

  1. You will need to install Node.js. Here is a link below:

https://nodejs.org/en/download/


  1. 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


  1. After installing npm, you have to initialize npm. In command line, you can enter:

npm init -y


  1. Now you have to install Express.js which is one of the many node packages. In command line, you can enter:

npm install express


Test-Instructions

To test the API, I recommend downloading Insomnia's API Platform and enter the following in Insomnia's URL:

GET http://localhost:3000/api/notes


POST http://localhost:3000/api/notes

Example POST body:

{
  "title":"Notes Title",
  "text":"notes text content"
}

*id is automatically generated so you do not need to enter id



DELETE http://localhost:3000/api/notes/:id


Example DELETE: The API request below will delete note with id = "1"

DELETE http://localhost:3000/api/notes/1


Technologies-Used


Contribution-Guidelines

To contribute, please follow these steps:

  1. Fork this repository.
  2. Create a branch: git checkout -b <branch_name>.
  3. Make your changes and commit them: git commit -m '<commit_message>'
  4. Push to the original branch: git push origin <project_name>/<location>
  5. Create the pull request.

Alternatively see the GitHub documentation on creating a pull request.


Cloning-Guidelines

To install this code, please use Github's guidlines to clone the repository


Github repository:

https://github.com/elliotpark410/Note-Taker


Screenshots


Screenshot of Homepage



Screenshot of Notes page



GIF-of-Application


Link to GIF of Application

https://drive.google.com/file/d/1-fN70Da55qfgyvn8uq_jFv9tV8EVjPYc/view



Code-Snippets

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);
  });
});


Learning-Points

  • 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


Authors

1. Elliot Park

https://github.com/elliotpark410

https://www.linkedin.com/in/elliot-park/


License-and-Acknowledgements

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!


Contact

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