Question: Project Description: This project is based on a picture voting game tutorial at: http://tutorialzine.com/2014/01/nodejs-picture-voting-game-part-1/. It uses express , express3-handler and nedb modules. Even though you

Project Description:

This project is based on a picture voting game tutorial at: http://tutorialzine.com/2014/01/nodejs-picture-voting-game-part-1/. It uses express, express3-handler and nedb modules. Even though you may not know express3-handler or nedb, you have known enough of Node.js applications to be able to run and debug this application.

You can run this project from command line or inside an IDE. The application runs at http://localhost:8080/. You should be able to vote on 10 candidates and see their standings. You can use different browsers to simulate different participants. Once a participant has voted on all 10 candidates, he/she cannot vote a second time.

Your job is to choose your favorite debugging tool/method to set as many breakpoints as necessary to investigate how this application works. Then answer the questions in the next section.

Question 1: After you start the application and before you start voting, what is the content of the two files photos and users under data directory? What is the content after you have voted? What do you think these two files are used for? (You can show screenshots of the file content.)

Question 2: If you want to modify this application to vote on your favorite Pokmons, assuming you have 10 Pokmon pictures ready, how would you do it? You can try it and show screenshots of your experiment.

Question 3: After a user has voted, he/she cannot vote again. But try deleting the content of the users file under the data directory and restart the application. Does that allow you to vote again then? Why?

/**  * This file defines the routes used in your application  * It requires the database module that we wrote previously.  */  var db = require('./database'), photos = db.photos, users = db.users; module.exports = function(app){ // Homepage app.get('/', function(req, res){ // Find all photos photos.find({}, function(err, all_photos){ // Find the current user users.find({ip: req.ip}, function(err, u){ var voted_on = []; if(u.length == 1){ voted_on = u[0].votes; } // Find which photos the user hasn't still voted on var not_voted_on = all_photos.filter(function(photo){ return voted_on.indexOf(photo._id) == -1; }); var image_to_show = null; if(not_voted_on.length > 0){ // Choose a random image from the array image_to_show = not_voted_on[Math.floor(Math.random()*not_voted_on.length)]; } res.render('home', { photo: image_to_show }); }); }); }); app.get('/standings', function(req, res){ photos.find({}, function(err, all_photos){ // Sort the photos all_photos.sort(function(p1, p2){ return (p2.likes - p2.dislikes) - (p1.likes - p1.dislikes); }); // Render the standings template and pass the photos res.render('standings', { standings: all_photos }); }); }); // This is executed before the next two post requests app.post('*', function(req, res, next){ // Register the user in the database by ip address var userIp=req.ip; users.insert({ ip: userIp, votes: [] }, function(){ // Continue with the other routes next(); }); }); app.post('/notcute', vote); app.post('/cute', vote); function vote(req, res){ // Which field to increment, depending on the path var what = { '/notcute': {dislikes:1}, '/cute': {likes:1} }; // Find the photo, increment the vote counter and mark that the user has voted on it. photos.find({ name: req.body.photo }, function(err, found){ if(found.length == 1){ photos.update(found[0], {$inc : what[req.path]}); users.update({ip: req.ip}, { $addToSet: { votes: found[0]._id}}, function(){ res.redirect('../'); }); } else{ res.redirect('../'); } }); } };

Question 4: If you put a breakpoint on line 72 of routes.js, what value do you see for variable userIp? Try different browsers and see if the values are different for different browsers.

Question 5: Explain the logic as to how the standing is sorted? (Hint: See Line # 56 of routes.js file.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!