Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shopping cart express+node middleware
var Cart = function(storedItems) {
var items = storedItems || {};
this.add = function(item, sku) {
var storedItem = items[sku];
if (storedItem == undefined) {
storedItem = items[sku] = {quantity: 0, item: item}
}
storedItem.quantity += 1;
};
// dumb name for now. figure out forEach stuff
this.to_a = function() {
var arr = [];
for (sku in items) { arr.push(items[sku]); }
return arr;
};
this.dump = function() { return items; };
};
// Exports
exports.dandee = {
cart: function(req, res, next) {
var cart = new Cart(req.session.cart);
req.cart = cart;
next();
req.session.cart = cart.dump();
return;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.