Code Monkey home page Code Monkey logo

express-file-crud's Introduction

Express CRUD with File Storage -- Books API

Write an Express app, that uses a file for persistent storage.

Expectations

  • Use ES6 wherever possible (e.g. let, const, destructuring and fat arrow functions)
  • Use the GitHub Workflow
  • Make regular commits
    • Make a commit every time you complete a feature
    • Give each commit a meaningful commit message
    • Don't expect the directions in this README to tell you when to commit
  • All file operations must be asynchronous
  • Your code must be perfectly indented. If we can't read it, we aren't going to grade it.
  • Work in your project directory at all times
  • All access to the data (both in the file and in memory) can only be done through your data_store module

Part 1: Set up your npm/Express project

  1. Fork and clone this repo
  2. touch app.js
  3. npm init -y
  4. npm install --save express
  5. echo node_modules > .gitignore
  6. git add .
  7. git commit -m "Initial project setup"

Part 2: Set up nodemon and your npm scripts

  1. npm install --save-dev nodemon
  2. Open package.json
  3. Inside scripts
  • add "watch": "`npm bin`/nodemon"
  • make sure that you have the trailing commas in the right places!
  1. npm run watch

Part 3: Create an Express server

  1. Open app.js
  2. Require Express
  3. Initialize an app
  4. Determine the port to use
  • The port to use could be passed in as the second command line argument
  • If not provided, default to 8000
  • Store the port in a const
  1. Use app.listen to bind and listen for connections on the above port
  2. Check that this works by running nodemon app.js in your directory
  3. Git add, commit, push

Part 4: Set up your database file

  1. mkdir db
  • db is short for "database"
  1. cd db
  2. touch seed.json
  • this is where our initial data (i.e. "seed data") is stored
  • this file will be used to reset our database file whenever we want to start over
  1. open seed.json and paste the following inside

    [
      {
        "id": 1,
        "author": "Marijn Haverbeke",
        "title": "Eloquent JavaScript"
      },
      {
        "id": 2,
        "author": "Nick Morgan",
        "title": "JavaScript for Kids"
      },
      {
        "id": 3,
        "author": "Kyle Simpson",
        "title": "You Don't Know JS",
      }
    ]
  2. Go back to your project directory

  3. Open package.json

  4. Inside scripts

  • add "reset": "cp seed.json data.json"
  • make sure that you have the trailing commas in the right places!
  1. npm run reset
  2. ls
  • notice that there is now a data.json in the db directory
  • db/data.json is the file that we are going to modify
  1. echo db/data.json >> .gitignore
  • database files are typically not checked into source control, because they can get large, have nothing to do with development, and might hold sensitive data

Part 5: Create a module called data_store

  1. Create a new file called data_store.js
  2. Open data_store.js
  3. Create an empty module in here. We will fill it in the following steps.
  4. Open app.js
  5. Require your data_store module

Part 6: Implement a function that reads the contents of the file into memory

  1. Open data_store.js
  2. Write a function called load_from_file that reads all the contents of the db/data.json file into memory
  • TIP: "into memory" means save it in a variable (use a global variable)
  • File read/write is slow. So we will work from memory as much as possible and only update the file when we have to.
  1. Export this function.
  2. Open app.js
  3. Call load_from_file before you call app.listen

Part 7: Implement a function that returns all the books

  1. Open data_store.js
  2. Write a function called get_all_books that returns an array of all the books that are in memory
  3. Export this function.

Part 8: Implement GET /api/books

  1. Define a GET route at /api/books
  2. The route should send a json response with an array of all the books
  • use your data store's get_all_books function to achieve this

Part 9: Implement finding a book by its ID

  1. Open data_store.js
  2. Write a function called get_book_by_id(id)
  • returns the book with that ID
  • if no book is found, return undefined
  1. Export this function.

Part 10: Implement GET /api/books/:id

  1. Define a GET route at /api/books/:id
  2. The route should send a json response with the book that has the given ID
  • use your data store's get_book_by_id(id) function to achieve this
  1. If there is no book with the given ID, respond with 404 Not Found

Part 11: Implement a function that updates the file with the current information

  1. Open data_store.js
  2. Write a function called write_to_file that writes all the books that are in memory, back into the file
  3. Make sure that you can read the data back out of the file by using load_from_file
  4. Do NOT export this function.
  • This function is going to be a "private" or "secret" function that only your module can use
  1. Do NOT export the above-mentioned variable
  • We want to restrict access to only the functions that we export

Part 12: Implement adding a new book (with a unique id) to the data store

  1. Open data_store.js
  2. Create a new global variable called LAST_ID
  3. When you call load_from_file, update LAST_ID to be the largest ID that was loaded from the file
  4. Write a function called add_book that:
  • takes an object as a parameter
  • gives it a unique ID
    • TIP: just add 1 to LAST_ID and use that
  • adds it to books that are already in memory
  • calls write_to_file to update the file
  • returns the added book (with its unique ID)
  1. Export this function

Part 13: Implement POST /api/books

  1. Install and use body-parser
  2. Get the body of the request and pass it to add_book
  3. The route should send a json response with the newly-created book

Part 14: Implement updating a book in the data store

  1. Open data_store.js
  2. Write a function called update_book that:
  • takes an ID as a parameter
  • takes an object as a parameter
  • finds the book with that ID
  • if it is not found, return undefined
  • updates that book to have the information in the object
  • do NOT update the ID
  • calls write_to_file to update the file
  • returns the updated book
  1. Export this function

Part 15: Implement PUT /api/books/:id

  1. Get the body of the request and pass it to update_book
  2. The route should send a json response with the newly-updated book
  3. If there is no book with the given ID, respond with 404 Not Found

Part 16: Implement deleting a book from the data store

  1. Open data_store.js
  2. Write a function called delete_book that:
  • takes an ID as a parameter
  • finds the book with that ID
  • if it is not found, return undefined
  • removes that book from the global variable
  • calls write_to_file to update the file
  • returns the removed book
  1. Export this function

Part 17: Implement DELETE /api/books/:id

  1. Define a DELETE route at /api/books/:id
  2. The route should send a json response with the book that was deleted
  • use your data store's delete_book function to achieve this
  1. If there is no book with the given ID, respond with 404 Not Found

Submit your work

  • Make sure you add, commit and push all your changes to your fork of this repo and pull request

STRETCH

Implement the data store using a Map

express-file-crud's People

Contributors

carlosnsr avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.