Some exercises from the Falsy Values workshops.
The good parts:
- HTTP server and client in same script
- Express cookies example
- Express routing example
- Express error handling
- Express middlewares example
- Simple HTTP proxy
| namespace = (target, name, block) -> | |
| [target, name, block] = [(if typeof exports isnt 'undefined' then exports else window), arguments...] if arguments.length < 3 | |
| top = target | |
| target = target[item] or= {} for item in name.split '.' | |
| block target, top |
| class Singleton | |
| # We can make private variables! | |
| instance = null | |
| # Static singleton retriever/loader | |
| @get: -> | |
| if not @instance? | |
| instance = new @ | |
| instance.init() | |
Some exercises from the Falsy Values workshops.
The good parts:
| var fs = require('fs'); | |
| var child_process = require('child_process'); | |
| var spawn = child_process.spawn; | |
| function openEditor(file) { | |
| var cp = spawn(process.env.EDITOR, [file], { | |
| customFds: [ | |
| process.stdin, | |
| process.stdout, | |
| process.stderr |
| /* | |
| A console.log that won't leave you hanging when node exits | |
| */ | |
| console.log = function(d) { | |
| var res = process.stdout.write(d + '\n'); | |
| // this is the first time stdout got backed up | |
| if (!res && !process.stdout.pendingWrite) { | |
| process.stdout.pendingWrite = true; |
| /* | |
| jQuery.extend extracted from the jQuery source & optimised for NodeJS | |
| Twitter: @FGRibreau / fgribreau.com | |
| Usage: | |
| var Extend = require('./Extend'); | |
| // Extend | |
| var obj = Extend({opt1:true, opt2:true}, {opt1:false}); |
| [ | |
| { | |
| id: 1, | |
| category_id: 1, | |
| merchant: { | |
| id: 1, | |
| title: 'BobsResturant', | |
| address: 'Edinburgh', | |
| telephone: '01311000011', | |
| latitude: 55.946312, |
| // Authentication middleware | |
| // ------------------------- | |
| // Shim for DNode's current version of socket.io, | |
| // which cannot pass `null` references, and turning | |
| // the mongoose model to a pure JSON object | |
| function shim(err, doc, fn) { | |
| if (!err) err = 0 | |
| if (doc) doc = JSON.parse(JSON.stringify(doc)) |
| // DNode Session | |
| // ============= | |
| var connect = require('connect') | |
| module.exports = function(opt) { | |
| var key = opt.key || 'connect.sid' | |
| , store = opt.store | |
| , interval = opt.interval || 120000 |
| /** | |
| # ms.js | |
| No more painful `setTimeout(60 * 4 * 3 * 2 * 1 * Infinity * NaN * '☃')`. | |
| ms('2d') // 172800000 | |
| ms('1.5h') // 5400000 | |
| ms('1h') // 3600000 | |
| ms('1m') // 60000 |