Skip to content

Instantly share code, notes, and snippets.

View ericf's full-sized avatar

Eric Ferraiuolo ericf

View GitHub Profile
@ericf
ericf / gist:6151890
Last active December 20, 2015 15:09
var async = require('async'),
exphbs = require('express3-handlebars'),
path = require('path'),
request = require('request'),
config = require('../config'),
helpers = require('../lib/helpers'),
invs = require('../lib/invitations'),
templates = config.dirs.emails,
@ericf
ericf / gist:6133744
Last active December 20, 2015 12:49

Proposal for Extending Express Apps

Creating npm packages which extend the functionality of Express apps has become a major thing we've been doing. There are several approaches we can take, from messing with the Express object prototypes, to creating a function in which an express app is passed in. After trying the former, I'm now a fan of the latter.

The Issues with Extending Express

Extending the Express object prototypes has issues. The running Node.js process may have multiple versions of express, and in order to extend the prototypes you need to require('express'). This means that you might get a different express module instance than the one the main app is created from.

Both approaches suffer from extending something more than once. Similar to how there may be multiple version of express in the running Node.js process, there can also be multiple copies of the extension modules. If the app developer needs to rely on a different version of an Express ext

@ericf
ericf / rsvp.js
Created May 14, 2013 17:46
A snippet from rsvp.js from my wedding site.
Y.Model.prototype.promiseSave = function (options) {
var model = this;
return new Y.Promise(function (resolve, reject) {
model.save(options, function (err, res) {
return err ? reject(err) : resolve(res);
});
});
};
@ericf
ericf / guests.js
Last active December 17, 2015 01:19
This is how I've been using node-postgres to create parameterized queries to PostgreSQL which may contain one or more updates to a row. Stay safe, but flexible :) https://github.com/brianc/node-postgres
var pg = require('pg'),
config = require('../config'),
GUEST_BY_ID = 'SELECT * FROM guests WHERE id=$1 LIMIT 1',
UPDATE_GUEST = 'UPDATE guests SET $UPDATES WHERE id=$1',
UPDATE_SCHEMA = {
title : true,
name : true,
Y.Guest = Y.Base.create('guest', Y.Model, [Y.ModelSync.REST], {
root: '/guests/'
});
Y.Guests = Y.Base.create('guests', Y.ModelList, [Y.ModelSync.REST], {
model: Y.Guest
});
Y.Invitation = Y.Base.create('invitation', Y.Model, [Y.ModelSync.REST], {
root: '/invitations/',
<!doctype html>
<html>
<head>
<meta charset="utf8">
<title>JSON</title>
</head>
<body>
<h1>JSON Test Page</h1>
#!/usr/bin/env node
var fs = require('fs'),
path = require('path'),
yui_module = process.argv[2] ? path.join(process.cwd(), process.argv[2]) : 'yui',
output = process.argv[3],
EXCLUDE_FILTERS = [
/^cookie/,
@ericf
ericf / gist:4558180
Last active December 11, 2015 06:18
{
"align_indent": false,
"alignment_chars":
[
"=",
":"
],
"alignment_space_chars":
[
"="
@ericf
ericf / 3.8.0.txt
Last active December 10, 2015 00:08
Starting benchmarks.
Base x 12,721 ops/sec ±1.62% (92 runs sampled)
MyBase x 10,975 ops/sec ±4.68% (82 runs sampled)
MyBase with 10 simple value attributes x 7,674 ops/sec ±3.96% (90 runs sampled)
MyBase with 20 varied attributes x 4,157 ops/sec ±2.24% (81 runs sampled)
BaseCore x 41,917 ops/sec ±4.44% (81 runs sampled)
MyBaseCore x 44,374 ops/sec ±2.22% (90 runs sampled)
MyBaseCore with 10 simple value attributes x 15,058 ops/sec ±2.21% (91 runs sampled)
MyBaseCore with 20 varied attributes x 5,083 ops/sec ±4.71% (73 runs sampled)
Finished.
var app = new Y.App();
app.notify = function (req, res, next) {
if (!this._route_evt) {
this._route_evt = this.publish('route', {
defaultFn: function () {
next();
}
});
}