lvvsndattasai / P6

Project 6
0 stars 0 forks source link

Project 6: Appserver and Database[^1]

Scrum Team

Product Owner

Scrum Master

Developers

In this project you will start up a database system and convert your Photo Sharing App you built in Project 5 to fetch the views' models from it. We provide you a new webServer.js supporting the same interface as Project 5's web server but it also establishes a connection to a database. This allows you to make your app into a legitimate full stack application.

Setup

You should have MongoDB and Node.js installed on your system. If not, follow the installation instructions in Project 0 now.

*IMPORTANT!*
Project 6 setup is different from the previous projects. You start by making a copy of your project5 directory files into a directory named project6. Into the project6 directory extract the contents of this zip file. This zip file will overwrite the files package.json, webServer.js, .eslintrc.json, and index.html and add several new files and directories. In the unlikely event you had made necessary changes in any of these files in your project5 directory you will need to reapply the changes after doing the unzip.

Once you have the Project 6 files, fetch the dependent software using the command:

npm install

If you run npm install without removing the node_modules folder, you may encounter warnings like:

npm WARN library_A requires a peer of library_B but none is installed. You must install peer dependencies yourself.

You may encounter the warnings because the dependencies we use may not match each other's requirements of peer dependencies. You can safely ignore these warnings.

For this and the rest of the assignments in the course we will be running all three tiers of the web application (browser, web server, database) on your local machine.

Start and initialize the MongoDB database

Make sure you have MongoDB up and running in your enviornment as described in the installation instructions. Since the mongod command doesn't return until the database is shutdown you will want to either run it in a separate window or as a background process (e.g. mongod (args) & on Linux/MacOS).

Once the MongoDB server is started you can load the photo app data set by running the command:

node loadDatabase.js

This program loads the fake model data from previous projects (i.e. modelData/photoApp.js) into the database. Since our app currently doesn't have any support for adding or updating things you should only need to run loadDatabase.js once. The program erases whatever is in the database before loading the data set so it is safe to run multiple times.

We use the MongooseJS Object Definition Language (ODL) to define a schema to store the photo app data in MongoDB. The schema definition files are in the directory schema:

These files are loaded both into the loadDatabase.js program where they are used to create the database and the webServer.js where they are used to access the database. Note: The object schema stored in the database is similar to but necessarily different from the models JavaScript objects used in the previous assignment. Familiarize yourself with these schema definitions.

Start the Node.js web server

Once you have the database up and running you will need to start the web server. This can be done with the same command as the previous assignments (e.g. node webServer.js). Start your web server with the command from your project6 directory:

node webServer.js

If you use the above command, remember to restart the web server after each change you make to the server code. You can also use nodemon, which will watch for any changes to the server code and automatically restart the web server:

nodemon webServer.js

If you ever encounter command not found: nodemon error when running nodemon, try to run

node_modules/.bin/nodemon webServer.js

After updating your Photo Share App with the new files from Project 6 and starting the database and web server make sure the app is still working before continuing on to the assignment.

Problem 1: Convert the web server to use the database

The webServer.js we give you in this project is like the Project 5 webServer.js in that the app's model fetching routes use the magic models rather than a database. Your job is to convert all the routes to use the MongoDB database. There should be no accesses to models in your code and your app should work without the line:

var models = require("./modelData/photoApp.js").models;

in webServer.js, Note that any console.log statements in webServer.js will print to the terminal rather than the browser.

Web Server API

As in Project 5 the web server will return JSON encoded model data in response to HTTP GET requests to specific URLs. We provide the following specification of what URLs need to be support and what they should return. Your web server should support the following model fetching API:

To help you make sure your web server conforms to the proper API we provide a test suite in the sub-directory test. Please make sure that all of the tests in the suite pass before submitting. See the Testing section below for details.

Your GET requests do not return exactly the same thing that the models functions return but they do need to return the information needed by your app so that the model data of each view can be displayed with a single FetchModel call. You will need to do subsetting and/or augmentation of the objects coming from the database to build your response to meet the needs of the UI. For this assignment you are not allow to alter the database schema in anyway.

*IMPORTANT!*

Implementing these Express request handlers requires interacting with two different "model" data objects. The Mongoose system returns models from the objects stored in MongoDB while the request itself should return the data models needed by the Photo App views. Unfortunately since the Mongoose models are set by the database schema and front end models are set by the needs of the UI views they don't align perfectly. Handling these requests will require processing to assemble the model needed by the front end from the Mongoose models returned from the database.

Care needs to be taken when doing this processing since the models returned by Mongoose are JavaScript objects but have special processing done on them so that any modifications that do not match the declared schema are tossed. This means that simply updating a Mongoose model to have the properties expected by the front end doesn't work as expected. One way to work around this is to create a copy of the Mongoose model object. A simple way of doing the copy is to translate the model into JSON and back to an JavaScript objects. The following code fragment does this object cloning:

JSON.parse(JSON.stringify(modelObject));

by taking modelObject converting into a JSON string and then converting it back to a JavaScript object, this time without the methods and special handling done on Mongoose models.

Problem 2: Convert your app to use axios

In preparation for the next assignment where we will use more of the REST API, convert your photo app to use axios rather than your own FetchModel routine to fetch the models from the web server. Axios is one of the many npm packages available for fetching data. It is faster and has more functionality than our FetchModel function, and it is what we will use to make our requests going forward. Your photo app should work without the FetchModel function definition after finishing this problem. The functionality of your app should be exactly the same before and after removing the FetchModel function definition -- make sure you do not break anything in the process of making this switch.

In particular, any time you call FetchModel in photoShare.jsx or your other components, you should replace it with a call to axios.get. See the axios documentation for more details.

Here are some hints for making the switch to axios:

import axios from "axios";

Notice that axios.get returns a Promise, much like FetchModel. We can attach success and failure handlers using .then and .catch respectively. It's important to handle both success and error cases for any requests. You shouldn't have to change the logic of your handlers from FetchModel, very much, or at all, when attaching them to axios.get.

Testing

Testing a full web application is challenging. In the directory test we provide a test of just the backend portion of your application. The test uses Mocha, a popular framework for writing Node.js tests. To setup the test environment, from inside the test subdirectory do an npm install to fetch Mocha and all the related dependencies. Once you have done this, you can run the test by running the command inside the test directory: npm test.

The npm test command runs the file test/serverApiTest.js which is a program written in the Mocha language (e.g. describe() and it()) testing the three Photo App backend URLs (/user/list, /user/ID, /photosOfUser/ID). In order to be reasonably sure that the functionality of the backend routes conforms to spec, please check that all our provided tests pass before submitting. A portion of your project evaluation will be based on how many of these tests you pass.

Extra Credit

Your Photo App’s marketing department has come up with a “small” tweak to the app to make it more social network friendly. The change is:

In implementing this you are welcome to add new server API calls or enhance existing calls. If you do so you need to update the Mocha test (test/serverApiTest.js) to test your new functionality. If you add new APIs include them in a new describe() block following the pattern used by the other tests. Make sure that the provided tests still pass before submitting. You should not add new properties to the Mongoose Schema but you are welcome to add any indexes you need to make this work on larger data sets.

For grading the course staff will enable the Advanced Features setting on your app (if present) and look for the count bubbles UI to determine whether or not they should grade you on the extra credit portion.

Style

These requirements will be met if your problem solutions have proper MVC decomposition. In addition, your code and templates must be clean and readable, and your app must be at least "reasonably nice" in appearance and convenience.

In addition, your code and templates must be clean and readable. Remember to run npm run lint before submitting. The linter should raise no errors.

Deliverables

Use the standard class submission mechanism to submit project6. Make sure your code is free of any lint warnings and passes the provided test suite.

[^1]: Stanford Computer Science