Skip to content

Instantly share code, notes, and snippets.

node helloworld.js
Server running at http://127.0.0.1:3000/
# At this point in another terminal I use netcat to open a plain TCP connection,
# and then Ctrl-C to close the connection without having sent any data.
# node then blows up with:
Segmentation Fault
var sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
setTimeout(function () {
res.sendHeader(200, {'Content-Type': 'text/plain'});
res.sendBody('Hello World');
res.finish();
}, 2000);
}).listen(3000);
sys.puts('Server running at http://127.0.0.1:3000/');
http.createServer(dispatchRequest).listen(port);
function dispatchRequest(req, res)
{
res.sentinel = new Sentinel(function (e) {
// Like a catch() clause that catches callbacks too.
// Called if handleRequest throws, or if any of the callbacks
// registered by handleRequest throw, or if any of the
#!/usr/bin/env node
var sys = require('sys');
process.exceptionCatcher = function (e) {
sys.puts('Catcher 1 catches "' + e.message + '"');
sys.puts('Catcher 1 returns');
}
Throwing from main
uncaughtException catches "thrown by main"
Timeout 1 throws
Catcher 1 catches "thrown by Timeout 1"
Catcher 1 returns
Timeout 2 throws
Catcher 2 catches "thrown by Timeout 2"
Catcher 2 throws
uncaughtException catches "thrown by Catcher 2"
var sys = require('sys');
var http = require('http');
var port = 3000;
http.createServer(dispatchRequest).listen(port);
sys.puts('Server running on port ' + port);
// This is the web framework, which wants to send
var express = require('express');
var app = module.exports = express.createServer();
app.get('/', function(req, res){
process.nextTick(function () {
nonexistantFunc();
});
});
@benw
benw / ms.js
Created December 21, 2011 00:57 — forked from rauchg/ms.md
Milliseconds conversion utility.
/**
# ms.js
No more painful `setTimeout(fn, 60 * 4 * 3 * 2 * 1 * Infinity * NaN * '☃')`.
ms('2d') // 172800000
ms('1.5h') // 5400000
ms('1h') // 3600000
ms('1m') // 60000
$('button.accept-terms').click(function (e) {
$(e.target).attr('disabled', 'disabled');
$.ajax({
type: 'POST',
url: '/settings',
data: {
_csrf: window._csrf,
accept_terms: true
},
success: function (data, textStatus, jqXHR) {
@benw
benw / list.rs
Created May 28, 2013 02:12
In which I learn to use the ref keyword.
enum List {
Nil,
Node(int, ~List)
}
fn walk(l: &List) -> ~str {
match *l {
Node(i, ref rest) => fmt!("%d %s", i, walk(*rest)),
Nil => ~"end"
}