Skip to content

Instantly share code, notes, and snippets.

View kevboutin's full-sized avatar

Kevin Boutin kevboutin

View GitHub Profile
@kevboutin
kevboutin / vindecoder.js
Last active April 23, 2024 11:09
Decode a VIN
/**
* VIN decoder.
*
* kevinboutin on 3/11/18.
*
* My VIN for testing is WBA3A5G59DNP26082 so use the following command to invoke:
* node vindecoder WBA3A5G59DNP26082
*
* Examples:
* KM8JM12D56U303366
@kevboutin
kevboutin / hapi-static-files.js
Created February 26, 2016 23:05
Shows how to serve up static files using hapi within node.
'use strict';
const Hapi = require('hapi');
const Path = require('path');
const server = new Hapi.Server();
server.connection({ port: 8000 });
// test by using this on browser:
// http://localhost:8000/hapi.png
server.register(require('inert'), () => {
@kevboutin
kevboutin / hapi-views.js
Created February 26, 2016 22:55
Shows how to use view templating with hapi within node.
'use strict';
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({ port: 8000 });
// test by using this on browser:
// http://localhost:8000/kevin
server.register(require('vision'), () => {
server.views({
@kevboutin
kevboutin / hapi-post-put.js
Created February 26, 2016 22:46
Shows POST and PUT examples in hapi within Node.
'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 February 26, 2016 21:34
Shows how to use hapi to extend lifecycle events within node.
'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 February 26, 2016 21:21
Shows how to configure and use friendly error pages using extension events in hapi within node.
'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 February 26, 2016 21:07
Shows how to use joi for request validation when using hapi within Node.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 February 26, 2016 20:57
Shows how to use hapi within node to manage state with cookies.
'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 January 30, 2016 15:51
Example webworker library
// 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 January 30, 2016 15:22
Calculate shortest distance between two points
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;