Skip to content

Instantly share code, notes, and snippets.

@kwyn
Last active August 29, 2015 14:10
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 kwyn/a60ee01408f39d87e48f to your computer and use it in GitHub Desktop.
Save kwyn/a60ee01408f39d87e48f to your computer and use it in GitHub Desktop.
var config = module.exports = {};
config.server = {
views: {
engines: {
jade: require('jade')
},
basePath: __dirname,
path: './templates',
partialsPath: './templates/partials',
layout: false
}
};
if( process.env.NODE_ENV === 'dev'){
config.server.debug = { request : ['error']};
}
var Hapi = require('hapi');
var routes = require('./routes.js');
var config = require('./config.js');
var userSessions = require('./services/users');
// load environment variables.
var dotenv = require('dotenv');
dotenv.load();
// Create a server with a host and port
var server = new Hapi.Server(process.env.HOST, parseInt(process.env.PORT), config.server);
server.ext('onPreHandler', function(request, next){
if( request.session.get('user') === undefined ){
request.session.set('user', {id: request.session.id});
}
next()
});
server.pack.register({
plugin: require('yar'),
options: {
cookieOptions: {
password: "its a password",
isSecure: false,
},
maxCookieSize: 0,
cache: {
engine: require('catbox-redis'),
host: '127.0.0.1',
port: '6379'
}
}
}, function(err){
if(err){
console.log(err)
}
// Add the route
server.route(routes);
if (!module.parent) {
server.start(function() {
console.log("Server started at " + process.env.HOST + " on port: " + process.env.PORT)
});
}
});
// Start the server
module.exports = server;
var path = require('path');
var _ = require('lodash');
var Cart = require(path.join(__dirname + './../../assets/js/cart'));
var inventory = require(path.join(__dirname + './../../inventory.json')); // This will replaced with a DB ideally.
var Hoek = require('Hoek');
module.exports = function (request, reply) {
var updatedCart = request.payload;
var newCart = new Cart(true);
_.forEach(updatedCart.items, function(item){
var inventoryItem = _.find(inventory, {name: item.name});
if( inventoryItem ){
newCart.addItem(inventoryItem, item.quantity);
}
});
var user = request.session.get('user');
user.cart = newCart.items;
request.session.set('user', user);
reply(newCart);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment