-
Notifications
You must be signed in to change notification settings - Fork 16
Problem Adding Truck #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Homework | ||
| * JSCRIPT300-Spring2015/Module_5 | ||
| * by Diane Zevenbergen | ||
| */ | ||
|
|
||
| var express = require('express'); | ||
| var app = express(); | ||
| var bodyParser = require('body-parser'); | ||
| var truckRoutes = require('./routes/truckRoutes'); | ||
|
|
||
| var port = process.env.PORT || 3000; | ||
|
|
||
| app.use(bodyParser.urlencoded({ extended: true })); | ||
|
|
||
| app.use(express.static('public')); | ||
|
|
||
| app.use('/trucks', truckRoutes); | ||
|
|
||
| app.listen(port, function() { | ||
| console.log('listening on port 3000'); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "name": "Module_5_HW", | ||
| "version": "1.0.0", | ||
| "description": "For this assignment, you're going to fork this repository to your own Github account and clone that repository locally. Inside the repo you'll find a placeholder for app.js, a placeholder routes/truckRoutes.js, a model/truckModel.js, and a 'public' directory. Inside the public directory you'll find an index.html file, a trucks.css file, and a client.js file.", | ||
| "main": "app.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/dianezev/Module_5.git" | ||
| }, | ||
| "author": "", | ||
| "license": "ISC", | ||
| "bugs": { | ||
| "url": "https://github.com/dianezev/Module_5/issues" | ||
| }, | ||
| "homepage": "https://github.com/dianezev/Module_5", | ||
| "dependencies": { | ||
| "body-parser": "^1.12.3", | ||
| "express": "^4.12.3", | ||
| "mongoose": "^4.0.2", | ||
| "underscore": "^1.8.3" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,26 @@ $(function () { | |
|
|
||
| 'use strict'; | ||
|
|
||
| /* | ||
| * QUESTION: I wanted to add underscore here in order to use | ||
| * _.sortBy to display trucks in alpha order, but I got the | ||
| * RefError 'require is not defined'. What am I doing wrong? | ||
| * | ||
| * var _ = require('underscore'); | ||
| */ | ||
|
|
||
| var foodTypes = []; | ||
|
|
||
| $.get('/trucks', function (truckList) { | ||
|
|
||
| /* | ||
| * This is where I wanted to sort but couldn't get | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just add a script reference to index.html that includes the underscore library:
Make sure it appears before the client.js script reference. |
||
| * my require statement above to work | ||
| * | ||
| * // Sort truckList, case-insensitive (from stackoverflow) | ||
| * truckList = _.sortBy(truckList, function (i) { return i.name.toLowerCase(); }); | ||
| */ | ||
|
|
||
| var list = []; | ||
| if (truckList) { | ||
| truckList.forEach(function (truck) { | ||
|
|
@@ -18,10 +35,10 @@ $(function () { | |
|
|
||
| e.preventDefault(); | ||
| var $form = $(this); | ||
|
|
||
| console.log(foodTypes); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure to remove console.log call before code goes to production. It's not as big of a deal as it used to be, but some older browsers, such as IE7, couldn't run the code because it had no console available unless the developer tools were open. This caused a script error that would break other scripts. |
||
| var truckData = { | ||
| name: $('[name=name]').val(), | ||
| type: foodTypes, | ||
| foodType: foodTypes, | ||
| schedule: getSchedule(), | ||
| description: $('[name=description]').val(), | ||
| payment: getPaymentTypes(), | ||
|
|
@@ -30,7 +47,7 @@ $(function () { | |
| Twitter: $('[name=Twitter]').val() | ||
| }; | ||
|
|
||
| $.ajax({ | ||
| $.ajax({ | ||
| method: 'POST', | ||
| url: '/trucks', | ||
| data: truckData | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,54 @@ | ||
| // connect to mongodb in this module as this is where you'll be making creat/read/delete calls to your database | ||
| // use 'mongodb://localhost/foodTruckAPI' for your mongoose connection string | ||
| // remember this is a Node module | ||
| /* | ||
| * Homework | ||
| * JSCRIPT300-Spring2015/Module_5 | ||
| * by Diane Zevenbergen | ||
| */ | ||
|
|
||
| var express = require('express'); | ||
| var router = express.Router(); | ||
|
|
||
| var mongoose = require('mongoose'); | ||
| var db = mongoose.connect('mongodb://localhost/foodTruckAPI'); | ||
| var bodyParser = require('body-parser'); | ||
|
|
||
| // Defining a model for Mongoose to use ('Truck' is instance of truckModel) | ||
| var Truck = require('../models/truckModel'); | ||
|
|
||
| router.route('/') | ||
| .get(function (request, response) { | ||
| Truck.find(function (error, trucks) { | ||
| if (error) { | ||
| response.status(500).send(error); | ||
| } else { | ||
| response.json(trucks); | ||
| } | ||
| }); | ||
| }) | ||
| .post(function (request, response) { | ||
| var truck = new Truck(request.body); | ||
| truck.save(); | ||
| response.status(201).send(truck); | ||
| }); | ||
|
|
||
| router.route('/:truckId') | ||
| .get(function (request, response) { | ||
| Truck.findById(request.params.truckId, function (error, truck) { | ||
| if (error) { | ||
| response.status(500).send(error); | ||
| } else { | ||
| response.json(truck); | ||
| } | ||
| }); | ||
| }) | ||
| .delete(function (request, response) { | ||
| Truck.findById(request.params.truckId, function (error, truck) { | ||
| if (error) { | ||
| response.status(500).send(error); | ||
| } else { | ||
| truck.remove(); | ||
| response.status(204).send('removed'); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| module.exports = router; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Require is a Node function, but this is a client-side script, so you're not using Node here.