Skip to content

Instantly share code, notes, and snippets.

@j-mcnally
j-mcnally / justinUtils.js
Created August 10, 2011 15:16
Node module with some useful utils i will ammend over time. Depends on underscore
module.exports = function() {
var justin = {
removeKeys: function(keys, obj) {
var newObj = {};
for (var i = 0; i < _.keys(obj).length; i++) {
var key = _.keys(obj)[i];
if (_.keys(obj).indexOf(key) == -1) {
newObj[key] = obj[key];
}
@j-mcnally
j-mcnally / preflightApi.js
Created August 10, 2011 21:03
Express Middleware for CORS access.
module.exports = function preflightApi(){
return function preflightApi(req, res, next){
//this is where the magic happens
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'User-Agent, Accept-Language, Accept-Encoding, Accept-Charset, Connection, Content-Length, Origin, Pragma, Accept-Charset, Cache-Control, Accept, Content-Type, Sessionid');
res.header('Access-Control-Max-Age', '1000');
@j-mcnally
j-mcnally / hooker.js
Created August 16, 2011 22:49
Beanstalk Deployment Hook Server
var http = require('http');
var sys = require('sys')
var exec = require('child_process').exec;
http.createServer(function (req, res) {
var header=req.headers['authorization']||'', // get the header
token=header.split(/\s+/).pop()||'', // and the encoded auth token
auth=new Buffer(token, 'base64').toString(), // convert from base64
parts=auth.split(/:/), // split on colon
username=parts[0],
password=parts[1];
@j-mcnally
j-mcnally / Readme
Created August 17, 2011 21:24
Nginx for API and Webapp
So you want to build a HTML Webapp and then query your JSON Api. To avoid cross domain calls use NGINX to proxy only certain requests to the API. In my case I use the Accept: header from the HTTP request to specify return data format. If that format is application/json send it to the api, otherwise try to serve the static html.
@j-mcnally
j-mcnally / pg.php
Created August 18, 2011 01:13
CodeIgniter Permalink Routing
<?php
//would be controllers/pg.php
class pg extends Controller {
var $pgItem;
var $parentPage;
function pg() {
//do some constructor related stuff here
}
function permalink($segments) {
@j-mcnally
j-mcnally / Readme
Created August 18, 2011 22:04
Express: Switch cookie sessions to a header session fore Node Apps built as APIs
The two files attached are Express Middleware
Srvformat sets the format based on the accept header
to switch it to api mode.
ApiSession uses the req.format set by this to decide
which kinds of sessions to use.
@j-mcnally
j-mcnally / Express-resources.js
Created August 18, 2011 22:29
Express-resources with middleware
/*!
* Express - Resource
* Copyright(c) 2010-2011 TJ Holowaychuk <[email protected]>
* Copyright(c) 2011 Daniel Gasienica <[email protected]>
* MIT Licensed
*/
/**
* Module dependencies.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.ajax("http://revolve.amazon/session/create", {
type: "POST",
dataType: "json",
processData: false,
headers: {
"Accept": "application/json",
@j-mcnally
j-mcnally / jsonForm.js
Created October 5, 2011 04:26
Expanded Serializer
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
var oldName = this.name;
this.name = this.name.replace(/\[\]/g, '');
if (this.name.indexOf('[') >= 0) {
var innerName = this.name.match(/.*\[(.*)\]/)[1];
@j-mcnally
j-mcnally / stop.sh
Created November 9, 2011 23:36
Kill a proccess you don't know the pid of
#!/bin/sh
pid=`ps aux | grep -i "/path/to/app/as/started" | grep -v "grep" | awk '{print $2}'`
kill $pid