Node & File IO

Story points 3
Tags Node FileIO
Hard Prerequisites
  • TOPICS: Node

  • You are required to create a back-end service that will help capture basic information about prospective students who come to inquire here at Umuzi. In this project you’ll just be storing and retrieving information from plain old json files.

    Instructions

    1. Create a class called Visitor. Instances of this class should have the following properties:
    • full name
    • age
    • date of visit
    • time of visit
    • comments
    • name of the person who assisted the visitor
    1. Create a function called save that saves the visitor’s data to a JSON file. The file name should be named like this visitor_{their_full_name}.json.
    alice.save()   # results in visitor_alice_cooper.json
    bob.save()     # results in visitor_bob_marley.json
    charlie.save() # results in visitor_charley_sheen.json
    

    Notice that the full name used in the file is all lower-case and spaces are replaced by underscores.

    1. Create a function called load that takes in a name and then grabs a Visitor object from file. It should simply console.log the visitor.

    eg:

    load("Alice Cooper")
    // prints out all of Alice's goodies
    
    
    load("Bob Marley")
    // Same deal for good ol Bob
    

    Resources

    Up for a challenge?

    Here are some upgrades you can add to your project if you are up for it.

    1. Update your load function so that it returns an instance of Visitor instead of just console.logging it. You’ll need to learn a little bit about Syncronous versus Asyncronous code to get this one right :)

    2. Make use of integer ids when saving things to files.

    Update your save function so it works like this:

    alice.save()   # results in visitor_1.json
    bob.save()     # results in visitor_2.json
    charlie.save() # results in visitor_3.json
    
    alice.comments = "Kinda weird, I don't think he'll fit in"
    alice.save()   # results in an UPDATE to visitor_1.json
    

    Your load function should also get a bit of an update.

    charlie = load(3)
    charlie.comments = "Winning!"
    charlie.save() # results in an UPDATE to visitor_3.json
    

    RAW CONTENT URL