Skip to content

Instantly share code, notes, and snippets.

@jeffwhelpley
jeffwhelpley / gist:5417758
Last active July 21, 2022 12:07
Getting node.js http-proxy to play nice with restify. This will also work for http-proxy with express or another node.js server. Note that I took various snippets of my code for this gist in order to try and convey the general idea. It won't work to just run this file.
/*
I have an existing node.js API server that runs on top of restify. I wanted to add http-proxy to forward
some calls to another API server. From the http-proxy examples out there, it seemed simply enough:
*/
var httpProxy = require('http-proxy');
var proxy = new httpProxy.RoutingProxy();
var restify = require('restify');
var server = restify.createServer();
@jeffwhelpley
jeffwhelpley / gist:6059715
Last active December 20, 2015 02:58
This is a design spec for a configuration-based Node.js NoSql security access library.

Node.js CRUD Configurator

The past 2 months I have been focused almost exclusively on building a new, exciting product for GetHuman. I am using the MEEAN stack (i.e. MongoDB Express.js ElasticSearch AngularJS Node.js), which basically means doing a boat load of JavaScript development. As someone who has done a lot of JavasScript development can tell you, there are a ton of open source libraries out there that can help speed your development and improve the quality of your code.

In fact, there are so many great libraries out there that I am always surprised when I can't find a good one for something that I think is a common problem. One such problem came up a couple weeks ago and I was inspired to create a solution which I hope to turn into a new open source library.

Problem

When an API is exposed with multiple roles and perhaps even multiple permission levels within each role, it takes a decent amount of code to properly control security access. For example, here are some examples of role based secu

@jeffwhelpley
jeffwhelpley / gist:6865807
Last active December 24, 2015 21:29
Idiomatic way in an AngularJS SPA to get data while navigating between pages

Before getting into the AngularJS way of doing things, from a general sense it is best to think of an SPA (single page app) in terms of states and transitions rather than pages and navigation. On the server side when you navigate to a page, all the data needed for that page is gathered at once and rendered in a template before sending the complete response back to the user. SPAs on the other hand can load the important initial data right away but then loading other non-essential data lazily.

In AngularJS that translates to two primary ways of loading data:

  1. Get the important data in the resolve() callback for the state/route transition
  2. Load other data lazily within the Controllers

Resolve

There are two primary ways for users to navigate within an AngularJS SPA. Misko has an example of using resolve() on the AngularJS router here:

@jeffwhelpley
jeffwhelpley / gist:9646517
Created March 19, 2014 17:15
JavaScript Multiple Inheritence
var Mom = function () {
this.skill = 'smart';
}
var Dad = function () {
this.interest = 'soccer'
}
Dad.prototype.getNickname = function () {
@jeffwhelpley
jeffwhelpley / gist:10041806
Last active August 29, 2015 13:58
Isomorphic JavaScript Templates

I am in the process of building an isomorphic JavaScript framework and I need some help deciding between two options for our templates.

I have two different strategies I am trying to choose between. I have both working and functionally either will be fine, but thing in particular I am trying to think about now is which one would be easier to grok and more accepted by developers/contractors we hire or community members we get to help out on open source pieces.

Note: both options using our jeff.js templating lib which is essentially JavaScript/JSON instead of HTML.

Option 1 - Node Style

The basic idea here is to have Node functions that are used to either bind data on the server side or generate HTML for the client side. For ex:

@jeffwhelpley
jeffwhelpley / gist:10170430
Last active August 29, 2015 13:58
Example isomorphic page
module.exports = {
/**
* Generate the model for the page. This is called before the page is rendered
* by both the client and the server.
*
* @param Q
* @param postService
* @returns {*}
*/
@jeffwhelpley
jeffwhelpley / gist:be850c3c05224e747212
Last active August 29, 2015 14:04
Avoiding peerDependencies issue
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"

check_run() {
	echo "$changed_files" | grep --quiet "$1" && eval "$2"
}

# if package.json is in the list of git changes, then refresh node_modules
check_run package.json "rm -rf node_modules"
check_run package.json "npm install"

I am returning to SoCal for the first time in 3 years after living there for 8 years. Here are my top 10 things to eat around the LA/Orange County area. I am hoping to hit all 10 of these:

  1. The Godmother at Bay Cities Deli, Santa Monica
  2. Fresh sangak at Wholesome Choice, Irvine
  3. Fried potatoes and falafel plate at Open Sesame, Long Beach
  4. Mole burrito and carnitas tostada from Hole Mole, Long Beach
  5. Bánh Mì at Che Cal, Garden Grove
  6. Egg tart and pastries at 85°C Bakery Cafe, Irinve
  7. [Burger and fries animal style at In 'n Out](http://
@jeffwhelpley
jeffwhelpley / gist:056e012544c631438ad2
Created July 12, 2015 02:06
playing nice: continuation-local-storage, hapi.js and pm2

I had a lot of issues trying to get continuation-local-storage (cls) working on a hapi.js app with pm2 in cluster_mode. Long story short, my mistake was wrapping cls around my server.start() when I should have been running it in a request handler. Here is the code you should use to get all this working:

var cls = require('continuation-local-storage');
var ns = cls.createNamespace('mySession');

var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({ port: 80 });
@jeffwhelpley
jeffwhelpley / sample.js
Last active September 24, 2015 14:43
Sample Angular Page
module.exports = {
model: function (postService, tokens, postUtil) {
return postService.find({ where: { urlId: tokens.urlId }, findOne: true })
.then(function (post) {
return {
pageHead: postUtil.getPageHeaderInfo(post),
post: post,
noindex: post.status === 'rejected' || !!post.duplicateOf
};
});