Skip to content

Instantly share code, notes, and snippets.

@dylants
dylants / app.js
Last active December 15, 2015 00:59
less middleware app.js
var lessMiddleware = require("less-middleware");
app.use(lessMiddleware({
src: __dirname + "/less",
dest: __dirname + "/public/css",
// if you're using a different src/dest directory, you
// MUST include the prefex, which matches the dest
// public directory
prefix: "/css",
// force true recompiles on every request... not the
@dylants
dylants / proxy-controller.js
Created July 16, 2013 21:24
node.js request proxy and pipe
/**
* Sends the output from the request to the response
*
* @param {Object} req The request
* @param {Object} res The response
*/
var pipeRequest = function(req, res) {
req.on("error", function(error) {
// Here we handle errors connecting to the server in the proxy
if (error && error.code === "ECONNREFUSED") {
@dylants
dylants / view.js
Created July 17, 2013 15:41
Backbone sync success and failure
return Backbone.View.extend({
...
events: {
"click #submit-button": "submit"
},
initialize: function() {
this.model.on( "sync", this.syncSuccess, this );
@dylants
dylants / Gruntfile.js
Created July 18, 2013 19:28
Gruntfile for mocha/jenkins integration
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks("grunt-mocha-test");
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: ["test-reports.xml"],
mochaTest: {
"test": {
@dylants
dylants / app.js
Created July 30, 2013 16:43
simple node proxy using http-proxy
var app, routingProxy, apiProxy, proxyHost, proxyPort;
var express = require("express"),
httpProxy = require("http-proxy"),
fs = require("fs");
// Should we dynamically define these by environment?
proxyHost = "localhost";
proxyPort = "8080";
// Create and define the Express server
@dylants
dylants / myapp.conf
Last active October 8, 2018 16:29
upstart config file for running node.js on a linux machine. This uses nodemon to allow file changes when pulled from git. This also specifies the NODE_ENV environment variable as "production" which would trigger the app to production mode (as opposed to development). Remove the environment variable if development environment is desired.
#!upstart
description "nodemon server for myapp"
author "ubuntu"
start on startup
stop on shutdown
script
export HOME="/home/ubuntu/git/myapp"
@dylants
dylants / code.js
Created August 13, 2013 22:44
Upload an image from a Backbone/jQuery client side app to a Node server
/*
* CLIENT SIDE CODE
*/
// perform a file upload
file = this.$("input[name='image-file']").prop("files")[0];
formData = new FormData();
formData.append("image", file);
$.ajax({
@dylants
dylants / modelAndSerializeCode.js
Last active December 21, 2015 03:38
Backbone form submission, serialize the form to set in model (form field names must match model variable names)
/*
* MODEL CODE
*/
formSubmit: function(ev) {
var formValues;
ev.preventDefault();
formValues = this.$("form").serializeObject();
this.model.save(formValues);
@dylants
dylants / backboneValidationSetup.js
Created August 15, 2013 18:42
Backbone Validation setup (with Backbone 1.0, backbone.validation plugin, and bootstrap integration)
/*
* MODEL
*/
return Backbone.Model.extend({
urlRoot: "/api/resource",
defaults: {
id: null,
name: null,
num: null,
@dylants
dylants / context.xml
Created August 22, 2013 20:56
Context.xml to be defined within a Java web application, under src/main/webapp/META-INF. This defines the database connection using JDBC within Tomcat. Defining this within the context.xml will configure the container, and allow the Java web app to connect via JNDI. This can also be included in the Tomcat server.xml, keeping configuration outsid…
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project>
<Context>
<Resource name="jdbc/web"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"