Skip to content

Instantly share code, notes, and snippets.

@aaronroberson
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronroberson/c0774a0c1a1257922f24 to your computer and use it in GitHub Desktop.
Save aaronroberson/c0774a0c1a1257922f24 to your computer and use it in GitHub Desktop.
Geekwise Day 7 Assetts
{
"directory": "public/bower_components"
}
/* ============== MODELS ========================== */
fs.readdirSync(__dirname + '/models').forEach(function(filename) {
if(~filename.indexOf('.js')) require(__dirname + '/models/' + filename)
});
/*
* Mongoose by default sets the auto_reconnect option to true.
* We recommend setting socket options at both the server and replica set level.
* We recommend a 30 second connection timeout because it allows for
* plenty of time in most operating environments.
*/
var options = {
server: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000
}
},
replset: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS : 30000
}
}
};
/*
* Mongoose uses a different connection string format than MongoDB's standard.
* Use the mongodb-uri library to help you convert from the standard format to
* Mongoose's format.
*/
var mongodbUri = 'mongodb://geekwiseuser:geekwise@ds045679.mongolab.com:45679/geekwise';
var mongooseUri = uriUtil.formatMongoose(mongodbUri);
var conn = mongoose.connection;
mongoose.connect(mongooseUri, options);
conn.on('error', console.error.bind(console, 'connection error:'));
conn.once('open', function() {
// Wait for the database connection to establish, then start the app.
});
{
"name": "mystore",
"version": "0.0.0",
"description": "Geekwise e-commerce application",
"main": "server.js",
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-connect": "~0.7.1"
},
"dependencies": {
"express": "~4.4.1",
"mongoose": "~3.8.12",
"mongodb-uri": "~0.9.6",
"morgan": "~1.1.1",
"cookie-parser": "~1.1.0",
"body-parser": "~1.3.0",
"debug": "~0.8.1",
"static-favicon": "~2.0.0-alpha"
}
}
(function(angular) {
"use strict";
var app = angular.module('MyStore');
app.controller('ProductDetail', function($scope, $stateParams, ProductService) {
// Set the id from the $stateParams to a local product_guid variable
var product_guid = $stateParams.id;
$scope.featuredProducts = [];
ProductService.getProduct(product_guid).then(function(response) {
$scope.product = response.data;
});
// Get the products from the product service
ProductService.getProducts().then(function(response) {
// Add the resulting array of products to a local products variable
var products = response.data;
// Loop through the products array using Angular's built-in forEach function
angular.forEach(products, function(product) {
if(product.guid !== product_guid && product.isFeatured) {
// Add the featured product to the featuredProducts array
$scope.featuredProducts.push(product);
}
});
});
});
})(window.angular);
(function(angular) {
"use strict";
var app = angular.module('MyStore');
app.factory('ProductService', function($http) {
// Angular factories return service objects
return {
// Object literal
getProduct: function(guid) {
return $http.get('/api/product/'+guid);
},
getProducts: function() {
// Return the promise
return $http.get('/api/products');
},
getProductFilters: function() {
// Return the promise
return $http.get('assets/json/product-filters.json');
}
}
});
})(window.angular);
// Require mongoose dependency
var mongoose = require('mongoose');
// Grab a referrence to the Schema
var Schema = mongoose.Schema;
// Create a new product schema
var productSchema = new Schema({
guid: String,
title: String
});
// Register the Product model and schema with mongoose
mongoose.model('Product', productSchema);
[
{
"id": 0,
"guid": "138ff4fd-ba1c-480b-9ef5-7fc32ea62c8f",
"isFeatured": true,
"isActive": true,
"isSpecial": true,
"price": "$1,366.15",
"specialPrice": "$545.24",
"picture": "http://placehold.it/300x250",
"thumbnail": "http://placehold.it/180x180",
"inventory": 26,
"color": "blue",
"gender": "male",
"title": "Product 1",
"manufacturer": "BUZZWORKS",
"description": "Minim aliquip ullamco deserunt elit amet ad velit voluptate amet cupidatat. Ad laboris in magna dolor eu. Dolor et voluptate ut tempor exercitation. Deserunt ipsum nulla occaecat proident ut voluptate nisi voluptate. Anim incididunt do aliqua tempor do Lorem ea non fugiat sit non laborum.\r\n",
"dateCreated": "2014-03-30T08:19:52 +07:00",
"dateUpdated": "2014-02-05T17:45:47 +08:00",
"tags": [
"exercitation",
"Lorem",
"amet",
"elit",
"deserunt",
"et",
"veniam"
]
},
{
"id": 1,
"guid": "bd91d124-e846-4fcc-81a3-c1e9a7144042",
"isFeatured": true,
"isActive": true,
"isSpecial": true,
"price": "$3,371.12",
"specialPrice": "$653.94",
"picture": "http://placehold.it/300x250",
"thumbnail": "http://placehold.it/180x180",
"inventory": 40,
"color": "green",
"gender": "male",
"title": "Product 2",
"manufacturer": "BOSTONIC",
"description": "Nulla id nostrud est velit qui eiusmod proident veniam esse ad qui. Sit nostrud velit ut velit sit incididunt tempor laborum aliqua velit. Aute laborum ex aliquip irure nostrud proident enim.\r\n",
"dateCreated": "2014-01-21T21:12:34 +08:00",
"dateUpdated": "2014-05-15T03:21:21 +07:00",
"tags": [
"fugiat",
"do",
"reprehenderit",
"id",
"cillum",
"aute",
"do"
]
},
{
"id": 2,
"guid": "750d29da-019d-4a94-a320-34954d74d811",
"isFeatured": true,
"isActive": true,
"isSpecial": false,
"price": "$1,521.79",
"specialPrice": "$479.08",
"picture": "http://placehold.it/300x250",
"thumbnail": "http://placehold.it/180x180",
"inventory": 25,
"color": "blue",
"gender": "male",
"title": "Product 3",
"manufacturer": "PROWASTE",
"description": "Non magna dolore Lorem aute ex voluptate. Ut nulla Lorem ex quis laborum enim. Id commodo aute nulla eu. Aliqua incididunt fugiat pariatur consectetur minim voluptate occaecat anim consectetur sint enim elit dolore. Aute elit sint ipsum laboris.\r\n",
"dateCreated": "2013-10-30T19:25:45 +07:00",
"dateUpdated": "2014-04-14T17:16:32 +07:00",
"tags": [
"est",
"consequat",
"anim",
"ullamco",
"minim",
"esse",
"tempor"
]
},
{
"id": 3,
"guid": "133e0248-410c-4e25-93ae-2f2e292c2dda",
"isFeatured": true,
"isActive": true,
"isSpecial": false,
"price": "$1,645.65",
"specialPrice": "$778.24",
"picture": "http://placehold.it/300x250",
"thumbnail": "http://placehold.it/180x180",
"inventory": 28,
"color": "green",
"gender": "male",
"title": "Product 4",
"manufacturer": "ZEAM",
"description": "Commodo do nulla fugiat consequat consectetur do sint minim quis anim anim ex quis. Et excepteur amet sint eiusmod enim dolor. Reprehenderit in tempor tempor sint ullamco consequat duis officia sunt ut. Consequat nulla cupidatat sit ipsum cillum ullamco ex officia eiusmod esse pariatur. Ad mollit eiusmod tempor deserunt sunt exercitation fugiat. Nostrud laborum aliqua ex ullamco elit. Ipsum nisi qui commodo sunt reprehenderit.\r\n",
"dateCreated": "2013-02-08T18:40:35 +08:00",
"dateUpdated": "2014-03-24T18:39:25 +07:00",
"tags": [
"do",
"labore",
"sit",
"id",
"ipsum",
"id",
"nisi"
]
},
{
"id": 4,
"guid": "602a134e-84fb-4771-98b9-ba6af8efbf6e",
"isFeatured": true,
"isActive": false,
"isSpecial": false,
"price": "$2,869.93",
"specialPrice": "$855.51",
"picture": "http://placehold.it/300x250",
"thumbnail": "http://placehold.it/180x180",
"inventory": 0,
"color": "red",
"gender": "male",
"title": "Product 5",
"manufacturer": "PHOLIO",
"description": "Ipsum pariatur sunt pariatur nulla adipisicing dolor irure anim dolor cillum. Qui elit ex aute sint. Tempor laborum ut amet Lorem excepteur nisi nostrud aute veniam ex consequat exercitation do id. Consectetur dolor eiusmod ullamco labore esse. Lorem excepteur pariatur cupidatat est reprehenderit velit dolore aute enim excepteur sint laborum irure. Cillum fugiat cupidatat anim aute mollit cupidatat nulla excepteur veniam ipsum nisi enim. Laborum labore aliquip eiusmod minim sint anim velit esse deserunt.\r\n",
"dateCreated": "2013-04-25T12:15:04 +07:00",
"dateUpdated": "2014-04-29T06:43:40 +07:00",
"tags": [
"eu",
"occaecat",
"cupidatat",
"Lorem",
"do",
"non",
"non"
]
}
]
module.exports = function(app) {
// Require mongoose dependency
var mongoose = require('mongoose');
/* ======================= server routes ====================== */
// handle things like api calls
// authentication routes
// product api route
app.get('/api/product/:productId', function(req, res) {
// use mongoose to get a product in the database by guid
mongoose.model('Product').findOne({guid: req.params.productId}, function(err, product) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err);
res.send(product); // return the product object in JSON format
});
});
// products api route
app.get('/api/products', function(req, res) {
// use mongoose to get all products in the database
mongoose.model('Product').find(function(err, products) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err);
res.send(products); // return all products in JSON format
});
});
// route to handle creating (app.post)
// route to handle delete (app.delete)
/* ========================= frontend routes ======================= */
// route to handle all angular requests
app.get('*', function(req, res) {
res.sendfile('./public/index.html'); // load our public/index.html file
});
};
module.exports = function(app) {
// Require mongoose dependency
var mongoose = require('mongoose');
/* ======================= server routes ====================== */
// handle things like api calls
// authentication routes
// product api route
// products api route
// route to handle creating (app.post)
// route to handle delete (app.delete)
/* ========================= frontend routes ======================= */
// route to handle all angular requests
app.get('*', function(req, res) {
res.sendfile('./public/index.html'); // load our public/index.html file
});
};
/* ================= REQUIRE MODULES ===================== */
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var uriUtil = require('mongodb-uri');
var path = require('path');
var fs = require('fs');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
/* ===================== CONFIGURATION ==================== */
var port = process.env.PORT || 9001; // Default port or port 9001
/*
* Mongoose by default sets the auto_reconnect option to true.
* We recommend setting socket options at both the server and replica set level.
* We recommend a 30 second connection timeout because it allows for
* plenty of time in most operating environments.
*/
var options = {
server: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000
}
},
replset: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS : 30000
}
}
};
/*
* Mongoose uses a different connection string format than MongoDB's standard.
* Use the mongodb-uri library to help you convert from the standard format to
* Mongoose's format.
*/
var mongodbUri = 'mongodb://<dbuser>:<dbpassword>@<host>:<port>/<dbname>';
var mongooseUri = uriUtil.formatMongoose(mongodbUri);
var conn = mongoose.connection;
mongoose.connect(mongooseUri, options);
conn.on('error', console.error.bind(console, 'connection error:'));
conn.once('open', function() {
// Wait for the database connection to establish, then start the app.
});
/* ======================== MODELS ======================== */
fs.readdirSync(__dirname + '/models').forEach(function(filename) {
if(~filename.indexOf('.js')) require(__dirname + '/models/' + filename)
});
/* ================= REGISTER MODULES ===================== */
app.use(favicon());
app.use(logger('dev')); // log every request to the console
app.use(bodyParser.json()); // have the ability to simulate DELETE and PUT
app.use(bodyParser.urlencoded()); // have the ability to simulate DELETE and PUT
app.use(cookieParser()); // have the ability to parse cookies
app.use(express.static(path.join(__dirname, 'public'))); // set the static files location
/* ======================== ROUTES ========================= */
require('./routes.js')(app); // configure our routes, passing in app reference
/* =============== START APP (THIS GOES LAST) ============== */
app.listen(port); // startup our app at http://localhost:9001
console.log('The MEAN app is served up at http://localhost:' + port); // shoutout to the user
exports = module.exports = app; // expose app
/* ================= REQUIRE MODULES ===================== */
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var uriUtil = require('mongodb-uri');
var path = require('path');
var fs = require('fs');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
/* ===================== CONFIGURATION ==================== */
var port = process.env.PORT || 9001; // Default port or port 9001
/* ======================== MODELS ======================== */
/* ================= REGISTER MODULES ===================== */
app.use(favicon());
app.use(logger('dev')); // log every request to the console
app.use(bodyParser.json()); // have the ability to simulate DELETE and PUT
app.use(bodyParser.urlencoded()); // have the ability to simulate DELETE and PUT
app.use(cookieParser()); // have the ability to parse cookies
app.use(express.static(path.join(__dirname, 'public'))); // set the static files location
/* ======================== ROUTES ========================= */
require('./routes.js')(app); // configure our routes, passing in app reference
/* =============== START APP (THIS GOES LAST) ============== */
app.listen(port); // startup our app at http://localhost:9001
console.log('The MEAN app is served up at http://localhost:' + port); // shoutout to the user
exports = module.exports = app; // expose app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment