Chris Pollett > Students >
Bui

    ( Print View)

    [Bio]

    [Blog]

    [CS 297 Proposal]

    [Dynamic Hashing Schemes - PDF]

    [WARC Files - PDF]

    [Deliverable 1]

    [Deliverable 2]

    [Deliverable 3]

    [Deliverable 4]

    [CS 297 Report - PDF]

    [CS 298 Proposal]

    [WARC-KIT Code]

    [CS 298 Report - PDF]

    [CS 298 Presentation - PDF]

A simple key-value store in Node.js

David Bui (david.bui01@sjsu.edu)

Purpose: For this deliverable the goal was to implement just a simple key-value store in Node.js. To accomplish this I set up a server using the Express.js server framework and a client using the frontend library React.js.

Client: In order to test the key-value store that was going to be implemented in the backend, a React page was setup in order to make testing easier. React's hot reloading system is a lot less intrusive compared to nodemon's (Another JS libary that enables hot reloading of servers) hot reloading which I used on the express server containing the key-value store. Making React perfect as a testing client for testing routes on the fly.

Client Webpage App.js

picture of client code

Server: The server is Express.js web server that implements routes that manipulate a persistent key-value store. For now the store itself is just one simple .json file that is manipulated by calling the routes below. In Deliverable 2 this store will be extended to implement a persistent Linear Hash Table

get route of store.js


import * as fs from 'fs';
import express from 'express';
const router = express.Router();

export const get = async (req, res)=> {

    let arr = Object.keys(req.body)
    let searchKey = JSON.parse(arr[0])

    fs.readFileSync('./resources/store.json', (err, data) => {
        if (err) {
            res.send("Error getting value associated with this key")
            return;
        };

        let store = JSON.parse(data);
        let value = store[searchKey];
        res.send(value)
    }
    )
}


createKVPair route of store.js


export const createKVPair = async  (req, res)=> {
    let arr = Object.keys(req.body)
    let insertObj = JSON.parse(arr[0])
    let toInsert = JSON.parse(insertObj['term'].toString());
    
    fs.writeFileSync('./resources/store.json', JSON.stringify(toInsert), (err) => {
        if (err) {
            console.error(err);
            res.send("Error creating the key value pair")
            return;
        };
        console.log("JSON file has been created");
        res.send('create KV pair successfully');
    });

}