Skip to content

Instantly share code, notes, and snippets.

View ovaillancourt's full-sized avatar

Olivier Vaillancourt ovaillancourt

  • Montreal, QC, Can
View GitHub Profile
@ovaillancourt
ovaillancourt / mountedApp.js
Created April 4, 2012 13:59
Mounted app working
var express = require('express');
var app = express.createServer();
someApp = express.createServer();
someApp.get('/', function(req, res) {
res.end('Reached / in someApp');
});
app.use('/foobar', someApp);
@ovaillancourt
ovaillancourt / SessionSubdomains.js
Created April 4, 2012 19:43
Configuring express session store to work across subdomain
app.use(express.cookieParser());
app.use(express.session({
secret: 'MySecret', //Should be the same across all the apps that share the same sessions.
store: Bla, //Init your store here
cookie: {domain: '.mydomain.com'} //The point in front here is important so that the same sessions work across all sub-domains.
}));
@ovaillancourt
ovaillancourt / usingAClosure.js
Created April 5, 2012 00:04
Using a closure or wrapped function
//That's your route
function(req,res,next){
var username = req.session.username;
var filteredArray = _.filter(myArray,function(value){
return value == username; //username is in your closure there...
});
}
@ovaillancourt
ovaillancourt / ObjectDotCreate.js
Created April 5, 2012 18:41
Prototype chains + constructor-based initialization using Object.create
//Animal constructor
function Animal( legCount ){
this.legCount = legCount;
}
//Methods
Animal.prototype.legCount = function(){
return( this.legCount );
};
@ovaillancourt
ovaillancourt / AddingAField.js
Created April 11, 2012 21:40
Adding a field to all sub-documents of a certain collection.
//Mongodb shell script
db.MyCollection.update(
{'comments._id' : {$exists : true}},
{$set : {'comments.$.author': PutYourValueHere}},
false,
true);
@ovaillancourt
ovaillancourt / magic.js
Created April 13, 2012 01:08
I love javascript
//Javascript magic here
//We grab the value from the dom element, split it in terms where every
//term is delimited by a comma, then trim every term, re-combine them
//together using a comma as separator and then URI encode the whole thing.
var fieldVals = encodeURIComponent(
this.$('#product_search #field_val').val().split(',').map(function(v){
return v.trim();
}).join(',')
);
@ovaillancourt
ovaillancourt / router.js
Created April 20, 2012 17:24
Router example
//The application model itself.
var Router = Backbone.Router.extend({
//Attributes
routes : {
//Home sweet home!
'home' : 'home',
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var FooSchema = new Schema({
dob: { type: Date},
num: {type : Number},
test : {
bla : {type: Number}
}
});
@ovaillancourt
ovaillancourt / mongooseDisconnectAndShutdown.js
Created May 15, 2012 00:09
Shutting down after a bunch of mongoose transactions.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var FooSchema = new Schema({
dob: { type: Date},
num: {type : Number},
});
Foo = mongoose.model('Foo', FooSchema);
@ovaillancourt
ovaillancourt / e3andsocketio.js
Created May 28, 2012 14:30
Express 3 and socketio
...
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server); //We're listening to the server here, not the express app.