Skip to content

Instantly share code, notes, and snippets.

@dylants
dylants / generateHref.js
Created April 3, 2014 19:42
Generate an href for a resource. Generates a full path to the resource in "development" environment, relative path in other environments ("production").
/**
* Generates an href to a resource depending on the environment. In development,
* this will be fully qualified based on the host. Otherwise it will be relative
* without any host information.
*
* @param {Object} app The express app object
* @param {Object} req The express request object
* @param {String} path The relative path to the resource, for example "/members/123"
* @return {String} An href to this resource
*/
@dylants
dylants / generateNum.js
Created April 3, 2014 16:31
Generate a number between low and high
/**
* Generates a number between low and high
*
* @param {Number} low The lower bound for the number generator
* @param {Number} high The upper bound for the number generator
* @return {Number} A number between low and high
*/
function generateNum(low, high) {
return Math.floor(Math.random() * ((+high) - (+low) + 1)) + (+low);
}
@dylants
dylants / Gruntfile.js
Created March 26, 2014 21:50
Mocha and Jenkins integration for Titanium (Appcelerator). The titanium-tests.js contains the setup for Jenkins integration with ti-mocha (Mocha integrated into Titanium). The Gruntfile.js contains a way of monitoring for the done.testing file, which will kill the iOS simulator and allow the Jenkins build to complete.
/* global module:true */
module.exports = function(grunt) {
"use strict";
grunt.initConfig({
watch: {
files: "done.testing",
tasks: ["shell"],
options: {
event: ["added"]
@dylants
dylants / nodejs
Created January 24, 2014 23:32
Nginx configuration file for sites-available to direct traffic based on subdomain to node servers, denying unknown subdomains
# The first node application
upstream app-one {
server 127.0.0.1:3000 max_fails=0;
}
# The second node application
upstream app-two {
server 127.0.0.1:3001 max_fails=0;
}
@dylants
dylants / app-state.js
Created January 7, 2014 21:57
JavaScript client side application state, using session storage.
/*global define:true, sessionStorage:true*/
define([
"underscore"
], function(_) {
"use-strict";
var appState, sessionStorageNamespace;
sessionStorageNamespace = "app.appState";
@dylants
dylants / authenticated-model.js
Created January 7, 2014 17:39
A Backbone Model which provides a hook to handle unauthorized API requests to the server. When dealing with server side security, this is one way of handling an unauthorized user on the client side in a generic way. By extending this model rather than Backbone's model, you can decide what should happen to the user when they hit an unauthorized r…
/* global define:true */
define([
"backbone"
], function(Backbone) {
"use strict";
// Extend Backbone's Model to override the sync function. Doing so allows us
// to get a hook into how the errors are handled. Here we can check if the
// response code is unauthorized, and if so, navigate to the login page
return Backbone.Model.extend({
@dylants
dylants / app.js
Last active February 6, 2021 17:50
Passport security using local authentication (username/password)
require("express-namespace");
var express = require("express"),
fs = require("fs"),
cons = require("consolidate"),
app = express(),
passport = require("passport"),
mongoose = require("mongoose");
// 30 days for session cookie lifetime
var SESSION_COOKIE_LIFETIME = 1000 * 60 * 60 * 24 * 30;
@dylants
dylants / app.js
Last active December 23, 2015 22:49
Node Express app.js (generic)
var express = require("express"),
fs = require("fs"),
cons = require("consolidate"),
app = express();
// configure the app (all environments)
app.configure(function() {
// read the port from the environment, else set to 3000
app.set("port", process.env.PORT || 3000);
@dylants
dylants / upload-data-controller.js
Created September 19, 2013 00:50
Upload CSV (Excel spreadsheet) data to a listening Node.js controller.
var request = require("request"),
csv = require("csv");
module.exports = function(app) {
// accepts the POST form submit of the CSV file
app.post("/upload/data", function(req, res) {
// the name under "files" must correspond to the name of the
// file input field in the submitted form (here: "csvdata")
csv().from.path(req.files.csvdata.path, {
delimiter: ",",
@dylants
dylants / java_version_control
Created August 26, 2013 19:52
Determine which version of Java you have running / Change the version of Java (these directions are for a Mac)
To list the Java versions installed on your machine (note the big V):
/usr/libexec/java_home -V
To change the version of Java (note the small v):
export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
export JAVA_HOME=`/usr/libexec/java_home -v 1.6`
The default JAVA_HOME is:
export JAVA_HOME=/Library/Java/Home