Skip to content

Instantly share code, notes, and snippets.

@klovadis
klovadis / gist:2549131
Created April 29, 2012 10:03
How to use optional arguments in node.js
// example function where arguments 2 and 3 are optional
function example( err, optionalA, optionalB, callback ) {
// retrieve arguments as array
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
// first argument is the error object
@klovadis
klovadis / gist:2549052
Created April 29, 2012 09:42
How to correctly raise errors in node.js
// example function that raises an error
function example (callback) {
// correct approach
return callback ( new Error('an error occurred') );
// NO! BAD KITTY!
return callback ('an error occurred');
@klovadis
klovadis / gist:2549029
Created April 29, 2012 09:35
Short circuit function chains if an error occurs
var fs = require('fs');
// read a file
function read_the_file(filename, callback) {
// begin by reading a file
fs.readFile(filename, function (err, contents) {
// an error occurred, i.e. the file was not found.
// instead of throwing an error, skip the other
@klovadis
klovadis / gist:2548942
Created April 29, 2012 09:22
Typical node.js callback pattern
// include the filesystem module
var fs = require('fs');
// fs.readFile: read a file and all its contents,
// then call a callback function
fs.readFile('/some/file', function (err, contents) {
// if any error occurred, throw it
if (err) throw err;
@klovadis
klovadis / index.js
Created April 6, 2012 15:32
How to include all routes in a folder for express using node-walker
// include the express framework
var express = require('express');
// include the node-walker module
// -> npm install node-walker
var walker = require('node-walker');
// the express server instance
var app = express.createServer();
<div id="navbar" class="navbar">
<ul id="navtabs" class="navtabs floatcontainer<vb:if condition="$show['member'] AND $notifications_total"> notify</vb:if>">
{vb:raw template_hook.navtab_start}
<vb:if condition="!$vboptions['selectednavtab'] AND THIS_SCRIPT != 'search'">
<li class="selected"><a class="navtab" href="{vb:link forumhome}">{vb:rawphrase forum}</a>
<ul class="floatcontainer">
{vb:raw template_hook.navbar_start}
<vb:if condition="$show['searchbuttons']">
<vb:if condition="$show['member']">
<li><a href="search.php?{vb:raw session.sessionurl}do=getnew&amp;contenttype=vBForum_Post">{vb:rawphrase new_posts_nav}</a></li>
@klovadis
klovadis / gist:1293127
Created October 17, 2011 17:12
Private class methods using the commonjs module system
// ----- throw your class into one single file = a seperate scope
// constructor and export
var myClass = module.exports = function () {
// ..
}
// a public method
myClass.prototype.publicMethod = function () {
@klovadis
klovadis / gist:1292642
Created October 17, 2011 13:51
CSS display property on pseudo-classes bug in webkit
<html>
<head>
<title>...</title>
<style>
<!--
p {
border-left:4px solid #00FF00;
display:block;