Skip to content

Instantly share code, notes, and snippets.

@ben-bradley
ben-bradley / errorHandler.js
Created September 29, 2014 14:24
hapi errorhandler
/* Errors
*
* This plugin handles formatting error messages.
* All traffic from the API should have a status code of 200
* and have the actual code embedded in the payload.
*/
module.exports = function (server, options) {
function errorHandler(plugin, options, next) {
plugin.ext('onPreResponse', function (request, reply) {
@ben-bradley
ben-bradley / session.js
Created September 29, 2014 14:24
hapi sessions with yar
module.exports = function (server, options) {
// https://github.com/hapijs/yar
var cookieOptions = {
name: '' + new Date().getTime(),
maxCookieSize: 1024, // 0 == server-side storage
cache: { // https://github.com/hapijs/hapi/blob/master/docs/Reference.md#plugincacheoptions
expiresIn: 1000 * 60 * 60 * 24
},
cookieOptions: {
password: '' + new Date().getTime(),
@ben-bradley
ben-bradley / mypackages.js
Created September 24, 2014 22:25
how to get a list of packages from a specific author
var registry = require('npm-stats')();
registry.user('ben-bradley').list(function (err, list) {
list.forEach(function (package) {
registry.module(package).version(function (err, details) {
console.log(package, details['dist-tags'].latest);
})
})
})
@ben-bradley
ben-bradley / string_prototype_getter.js
Created August 29, 2014 18:27
adding getters to prototypes
String.prototype.__defineGetter__('blargh', function() { return 'honk'; });
console.log("calling .blargh on this string will result in 'honk'".blargh); // => 'honk'
@ben-bradley
ben-bradley / .bashrc
Last active November 20, 2020 19:59
shell prompt
# Shell prompt stuff
function dashes() {
COLS=$(tput cols)
D=${PWD/$HOME/\~}
U=$(whoami)
H=$(hostname -s)
FLEN=16 # constant for spaces & timestamp
CLEN=$(( ${#D}+${#U}+${#H}+$FLEN ))
@ben-bradley
ben-bradley / globRequire.js
Created August 6, 2014 18:33
require all .js files in a directory on the fly
var glob = require('glob');
glob.sync(__dirname+'/models/*.js').forEach(require);
@ben-bradley
ben-bradley / chainserial_demo.js
Last active August 29, 2015 14:04
A quick demo of ChainSerial
var SerialChain = require('serialchain');
var chain = new SerialChain({
methodA: function (a, done) {
setTimeout(function () {
done(null, a);
}, 100);
}
});
@ben-bradley
ben-bradley / exec_env.js
Created August 1, 2014 19:47
correctly handle environment vars in a child_process
var env = process.env,
exec = require('child_process').exec;
exec('echo $MYVAR', { env: env }, function(err, stdout, stderr) {
if (err)
throw err;
console.log('STDOUT: ' + stdout); // => '\n'
console.log('STDERR: ' + stderr); // => ''
});
@ben-bradley
ben-bradley / chainIt.js
Created July 31, 2014 07:09
chaining async methods
var Chain = require('./');
var chain = new Chain();
chain.configure()
.thingOne('one')
.thingTwo('two')
.thingThree('three')
.done(function (err, results) {
console.log(results); // => [ 'one', 'two', 'three' ]
@ben-bradley
ben-bradley / installNode.sh
Created July 31, 2014 04:24
How to get NodeJS installed on VyOS
#!/bin/bash
sudo su -
mkdir /opt/node
cd /opt/node
wget http://nodejs.org/dist/node-latest.tar.gz
tar zxvf ndoe-latest.tar.gz
cd node-v*
./configure
make