-
Notifications
You must be signed in to change notification settings - Fork 67
Home
Due: Thursday 4/7, 12:00pm
The purpose of this assignment is to learn basic setup for a web application based on Node.js. This assignment is divided into two major parts: (1) Node.js configuration, and (2) MongoDB setup. By the end of this assignment, students should be comfortable handling node_modules, package.json, and database. This is an individual assignment.
- This assignment should be done individually.
- Given boilerplate is broken (intentionally). This is to enforce learning of how Node.js/Express works. You may use labs from COGS120/CSE170 to help fix these bugs. http://d.ucsd.edu/class/intro-hci/2016/lab/lab4/
- Refresh technical materials from prerequisite course (COGS 120)
- Understand basics of Node.js (dealing with
package.jsonandnode_modules) - Utilize MongoDB to create a simple application
Create a GitHub account (If you already have one, great!) http://www.github.com
Set up your programming environment:
- Mac users, please refer to Mac Setup Instructions.
- Windows users, please refer to Windows Setup Instructions.
- Linux users:
- Install Node.js, git, MongoDB with your favorite package manager and test it out. Then install Heroku Toolbelt https://toolbelt.heroku.com/
Create a Heroku account (If you already have one, great!) http://www.heroku.com/ You will need to link a credit card to it to use their databases (but as long as you use the free tier, you will never get charged).
Part I primarily focuses on how to manage node modules and execute Node.js server. This guideline does not guarantee to lead you to a solution, but rather help you figure out how to get started. You may use https://docs.npmjs.com/files/package.json, https://www.youtube.com/watch?v=AcA9SgAwgBo, Piazza, and other online resources to solve your problem(s).
-
Grab the starter code via GitHub: Fork and clone the boilerplate from this repository: https://github.com/WeibelLab-Teaching/cogs121-sp16-ass0
-
Create a package.json: On your terminal or command prompt, type
npm initinside the cogs121-sp16-ass0 directory- Fill-in appropriate information.
- If you do not know just press "enter" or "return"
- This will generate
package.jsonfile at the root directory of your project
-
Install some node modules and record them as dependencies: Install following Node.js dependencies: express (4.13.4), express-handlebars (3.0.0), body-parser (1.15.0)
- e.g.
npm install express --save - Try installing without
--saveand checkpackage.json(nothing should happen) - Then try installing with
--saveand checkpackage.json(something should have changed)
- e.g.
-
Experiment with utilizing the dependencies in package.json:
- Delete the node_modules folder which contains all the installed dependencies e.g.
rm -rf node_modules - Re-install all dependencies listed in your package.json using
npm install - Voila! All your node modules are back
- You can see how this is very useful for package managing, especially if multiple people work on the same project on multiple machines (in fact, npm stands for node package manager)
- Delete the node_modules folder which contains all the installed dependencies e.g.
-
Add a start script (documentation on scripts here): Open
package.jsonand add"start": "node server.js"underscripts"scripts" : { "start": "node server.js" }- Notice that the "start" script now runs the command, "node server.js"
-
Run the script and fix errors: Try
npm starton your terminal or command prompt ...- Yes, it should throw error(s).
- As mentioned above, this boilerplate is broken. Your job is to fix them. How?
- You know you have fixed your issues if you see:
Express server listening on port 3000on your terminal or command prompt - You can close the server by pressing ctrl+c
-
View the web app locally: Open an internet browser, and go to
localhost:3000.- You should see an ugly looking form with "email" and "content label.
- There's no functionality for this form yet.
Part II emphasizes on how to put MongoDB and Node.js together using Mongoose. For this assignment, MongoDB will primary framework for database; later in the course, you will explore other mechanisms of database such as SQL. You may use http://d.ucsd.edu/class/intro-hci/2016/lab/lab_db/, http://mongoosejs.com/docs/, Piazza, or other online resources to solve your problem(s).
-
Add modules for MongoDB: Install following Node.js dependencies: mongodb (1.4.30), mongoose (3.8.23)
- Refer to Part I on how to install Node.js dependencies
- Make sure all of your Node.js dependencies are shown in
package.json
-
Add directories for MongoDB: Create two directories under root directory of your project; each named
dbpathandlog. -
Add a prestart script (run automatically when you run "npm start"): Open
package.jsonand add"prestart": "mongod --dbpath dbpath --logpath log/mongod.log&"underscripts"scripts" : { "prestart": "mongod --dbpath dbpath --logpath log/mongod.log&", "start": "node server.js" } -
Add the database connection: Open
server.jsand uncomment "Database Connection" section.- Try
npm startagain. If there are any more errors, fix them. After that, the server should be running along with the message "Database connected successfully." - Close the server.
- If you see the following message on your terminal, you may ignore that message
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } js-bson: Failed to load c++ bson extension, using pure JS version - Try
-
Add the database model: Create
models.jsunder root directory of your project- Create a schema that has attributes of “email” (String), “content” (String), and “created” (Date)
- Make sure they export (e.g.
exports.Message = Mongoose.model('Message', MessageSchema);)
This is a good time to learn about the MVC pattern (model view controller). In our case, you can think of the structure like this:
- models.js as the model
- views/index.html as a view
- server.js + routes/index.js as the controller
-
Write code that tells the server what to do when a post request ot made to /message: Add a route "/message" with post request in your
server.js- e.g.
app.post("/message", router.message.send); - Note: the router setup is a little bit different from COGS 120, pay close attention to the router variable and how it is used in server.js
- e.g.
-
Add the route that step 6 uses: Create a
message.jsunderroutesdirectory of your project- This file should be responsible for receiving contents from a POST request and save it to database (Message schema)
- After saving the contents, it should redirect back to
/ - You can obtain POST request contents from
req.body(for GET request, it would bereq.paramsorreq.query) - Here is the starter code for
message.js. It is your job to figure out how to solve this portion
var models = require('../models'); exports.send = function(req, res) { console.log(req.body); // help you see what is inside of req.body // your solution here }; -
Render the database data on the / page: Modify
index.jsunderroutesdirectory of your project- Specifically, modify a function of
exports.view - This function should grab all the information from Message schema render it with those data
- Hint: look at
view/index.htmlto see how you might want to format the data
- Specifically, modify a function of
-
Account for errors: For both 7 and 8, make sure to do error handling
- You can look back at the COGS 120 lab and see how errors are handled there, specifically, you should log when errors occur
-
Test and see that your code works: Type
npm startand checklocalhost:3000- You should be able to type and submit your email and content through your form
- When you submit, you should see your submitted message! (Yay!) If you submit more messages, you should be able to see all of them displayed below the form
- If you encounter any errors, it is your responsibility to fix them
Once you are done, your assignment should be saved under your own Assignment0 GitHub repository and personal Heroku link.
- As refresher, you may refer to http://d.ucsd.edu/class/intro-hci/2016/lab/lab1/ for basic instructions on source control. You may ignore vagrant portion.
- For instructions on Heroku, you may refer to http://d.ucsd.edu/class/intro-hci/2016/lab/lab2/ starting at slide 17.
- In addition to this, you also need to type
heroku addons:add mongolabon terminal or command prompt - Make sure "mLab MongoDB" is available under your Heroku app's resources.
- In addition to this, you also need to type
Part III is optional, but is a critical learning process for this course. It is highly recommend that you go through this part if time permits. This part of the assignment is flexible with no strict guideline. Your job is to do following:
- Redesign this web application. It is expected that as a designer to improve an application for better usability, interface design, etc.
- You may also attempt to change aesthetics of this application with CSS and/or (client-side) JavaScript
- You may choose to use any CSS and JavaScript framework for this assignment
- ** CSS:** Bootstrap, Foundation, Flat-UI, SASS/LESS, Bourbon, etc. (or no framework is fine too)
- ** JavaScript:** Angular, React, Underscore, JQuery, Backbone, etc. (or vanilla JS is fine too)
Please submit your assignment via this Google Form: http://goo.gl/forms/5jUd5mo2w6
Because we will be going over groups in class on Thursday, for now, if you are in a 1PM section, fill out the form for Brian's section and if you are in a 2PM section, fill out the form for Jesse's section.