Skip to content

Instantly share code, notes, and snippets.

Avatar

Kevin Boutin kevboutin

View GitHub Profile
@kevboutin
kevboutin / hapi-post-put.js
Created Feb 26, 2016
Shows POST and PUT examples in hapi within Node.
View hapi-post-put.js
'use strict';
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({ port: 8000 });
// test by using this on command line:
// http -v --form POST localhost:8000 fname=Kevin lname=Boutin
server.route({
method: ['POST', 'PUT'],
@kevboutin
kevboutin / hapi-lifecycle-events.js
Created Feb 26, 2016
Shows how to use hapi to extend lifecycle events within node.
View hapi-lifecycle-events.js
'use strict';
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({ port: 8000 });
// test by using this on command line:
// http GET localhost:8000
server.ext('onRequest', (request, response) => {
console.log('onRequest');
@kevboutin
kevboutin / hapi-friendly-error-pages.js
Last active Feb 26, 2016
Shows how to configure and use friendly error pages using extension events in hapi within node.
View hapi-friendly-error-pages.js
'use strict';
const Hapi = require('hapi');
const Boom = require('boom');
const server = new Hapi.Server();
server.connection({ port: 8000 });
// test by using this in browser:
// http://localhost:8000
server.register(require('vision'), () => {
@kevboutin
kevboutin / hapi-request-validation.js
Created Feb 26, 2016
Shows how to use joi for request validation when using hapi within Node.js.
View hapi-request-validation.js
'use strict';
const Hapi = require('hapi');
const Joi = require('joi');
const server = new Hapi.Server();
server.connection({ port: 8000 });
// test by calling:
// http POST localhost:8000/user/123?id=456
// or for validation failure testing, call:
// http POST localhost:8000/user/123?id=foo
@kevboutin
kevboutin / hapi-cookies.js
Created Feb 26, 2016
Shows how to use hapi within node to manage state with cookies.
View hapi-cookies.js
'use strict';
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({ port: 8000 });
server.state('hello', {
//ttl = time to live
ttl: 60 * 60 * 1000,
isHttpOnly: true,
encoding: 'iron',
@kevboutin
kevboutin / webWorker.js
Created Jan 30, 2016
Example webworker library
View webWorker.js
// simple implementation of a thread pool
function Pool(size) {
var _this = this;
// set some defaults
this.taskQueue = [];
this.workerQueue = [];
this.poolSize = size;
this.addWorkerTask = function(workerTask) {
@kevboutin
kevboutin / calculateDistance.js
Last active Jan 30, 2016
Calculate shortest distance between two points
View calculateDistance.js
function calculateDistance(lat1, lon1, lat2, lon2) {
var R = 6371; // km
var dLat = (lat2 - lat1).toRad();
var dLon = (lon2 - lon1).toRad();
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d;
@kevboutin
kevboutin / shop.html
Last active Dec 19, 2015
An example of how to handle the display of multiple products/items on a page in reponsive grid system. The media queries are not really required but they are there in case other things on the page will interact responsively.
View shop.html
<!DOCTYPE html>
<html>
<head>
<title>Product Shopping View Example</title>
<style>
/*
Original idea:
http://www.barrelny.com/blog/text-align-justify-and-rwd/
http://codepen.io/patrickkunka/pen/GECBF
*/
@kevboutin
kevboutin / gist:5768004
Created Jun 12, 2013
This Javascript will print out response headers to the console.
View gist:5768004
// https://github.com/bgrins/devtools-snippets
// Print out response headers for current URL
(function() {
var request=new XMLHttpRequest();
request.open('HEAD',window.location,false);
request.send(null);
var headers = request.getAllResponseHeaders();
@kevboutin
kevboutin / performance.conf
Last active Dec 15, 2015
This is a default addition to httpd.conf via Include for performance purposes.
View performance.conf
# ----------------------------------------------------------------------
# Better website experience for IE users
# ----------------------------------------------------------------------
# Force the latest IE version, in various cases when it may fall back to IE7 mode
# github.com/rails/rails/commit/123eb25#commitcomment-118920
# Use ChromeFrame if it's installed for a better experience for the poor IE folk
<IfModule mod_headers.c>
Header set X-UA-Compatible "IE=Edge,chrome=1"