Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
22 changes: 22 additions & 0 deletions app.js
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');
});
27 changes: 17 additions & 10 deletions models/truckModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
// name your model 'Truck' (see the example in the slide deck for 'Book')
// this is to match the name of the collection the data was imported into

// var foodTruckSchema = new Schema({
// name: String,
// foodType: [String],
// schedule: [String],
// payment: [String],
// description: String,
// website: String,
// Facebook: String,
// Twitter: String
// });
//Diane added these two lines????
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// Diane changed 'foodTruckSchema' to 'truckModel'
var truckModel = new Schema({
name: String,
foodType: [String],
schedule: [String],
payment: [String],
description: String,
website: String,
Facebook: String,
Twitter: String
});

module.exports = mongoose.model('Truck', truckModel);
25 changes: 25 additions & 0 deletions package.json
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"
}
}
23 changes: 20 additions & 3 deletions public/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Contributor

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.

*/

var foodTypes = [];

$.get('/trucks', function (truckList) {

/*
* This is where I wanted to sort but couldn't get
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

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) {
Expand All @@ -18,10 +35,10 @@ $(function () {

e.preventDefault();
var $form = $(this);

console.log(foodTypes);
Copy link
Contributor

Choose a reason for hiding this comment

The 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(),
Expand All @@ -30,7 +47,7 @@ $(function () {
Twitter: $('[name=Twitter]').val()
};

$.ajax({
$.ajax({
method: 'POST',
url: '/trucks',
data: truckData
Expand Down
57 changes: 54 additions & 3 deletions routes/truckRoutes.js
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;