Skip to content

Instantly share code, notes, and snippets.

View brianleroux's full-sized avatar
💚
You are now aware that you are breathing

Brian LeRoux brianleroux

💚
You are now aware that you are breathing
View GitHub Profile
@brianleroux
brianleroux / https-redir-middleware.js
Created April 20, 2017 17:48
express middleware for redirecting to http if a request to nginx came in from http
module.exports = function HTTPSRedirect(req, res, next) {
var env = process && process.env && process.env.NODE_ENV
if (env !== 'production' || req.headers['x-forwarded-proto'] === 'https' || req.url === '/health') {
next()
}
else {
res.redirect('https://' + req.hostname + req.url);
}
}
@brianleroux
brianleroux / minimalist.js.md
Last active March 3, 2017 12:23
Essential JS
  1. break
  2. catch
  3. delete
  4. else
  5. for
  6. function
  7. if
  8. return
  9. throw
  10. try
function add(a, b) {
if (!a) throw ReferenceError('a is undefined')
if (typeof a != 'number') throw TypeError('a not a number')
if (!b) throw ReferenceError('b is undefined')
if (typeof b != 'number') throw TypeError('b not a number')
return a + b
}
add(1, 2)
var assert = require('@smallwins/validate/assert')
function add(params) {
assert(params, {
'a': Number,
'b': Number,
})
return params.a + params.b
}
@brianleroux
brianleroux / node-add.js
Last active February 5, 2017 05:22
Exceptional circumstances: preventing programmer failure at runtime
var assert = require('assert')
function add(a, b) {
assert(typeof a === 'number', 'a is not a number got ' + typeof a)
assert(typeof b === 'number', 'b is not a number got ' + typeof b)
return a + b
}
add(1, 2)
@brianleroux
brianleroux / wtf-sns-apns.js
Created August 5, 2016 23:00
Send a silent push notification to APNS with AWS SNS.
sns.publish({
TargetArn: device.arn,
MessageStructure: 'json',
Message: JSON.stringify({
default: 'you will never see this muah!',
APNS_SANDBOX: JSON.stringify({
aps: {
'alert': '',
'content-available': 1
},
@brianleroux
brianleroux / kinda-how-i-use-tape.js
Last active January 22, 2016 17:35
sorta how i do
var test = require('tape')
var thingo = null
var mock = null
test('setup', t=> {
t.plan(1)
thingo.init()
t.ok(true, 'things are setup')
})
// Node style module writ in ES6 syntax:
exports default foo = () => console.log(‘hi’)
// Unfortunately “literally” transpiles to this:
exports.default = function foo() {
return console.log(‘hi’)
}
// Which means this will fail:
var foo = require(‘./foo’)
// this is an AMD module
define(function () {
return something
})
// and this is CommonJS
module.exports = something
<!DOCTYPE HTML>
<html>
<head>
<meta charset=utf-8>
<title>tests</title>
</head>
<body>
<div id=out></div>
<script src=test-echo-browser.js>
</script>