Skip to content

Instantly share code, notes, and snippets.

var setNestedJSON = function(on, path, value) {
if (!(path instanceof Array)) {
path = path.split('\.');
}
if (path && path.length > 0) {
var current = on;
var key = null;
while((key = path.shift()) != null) {
var http = require('http');
var requestCounter = 0;
var startedRequests = 0;
var endedRequests = 0;
http.createServer(function (request, response) {
request.number = requestCounter++;
console.log("#" + request.number + " started");
@FrankGrimm
FrankGrimm / nodepost-1.js
Created November 9, 2010 15:31
HTTP body in node.js
var http = require('http');
http.createServer(function (req, res) {
// set up some routes
switch(req.url) {
case '/':
console.log("[501] " + req.method + " to " + req.url);
res.writeHead(501, "Not implemented", {'Content-Type': 'text/html'});
res.end('<html><head><title>501 - Not implemented</title></head><body><h1>Not implemented!</h1></body></html>');
break;
@FrankGrimm
FrankGrimm / formexample.js
Created November 7, 2010 22:08
Basic example on how to get the body data of a HTTP request in node.js
var http = require('http');
var querystring = require('querystring');
var utils = require('utils');
http.createServer(function (req, res) {
switch(req.url) {
case '/':
res.writeHead(200, "OK", {'Content-Type': 'text/html'});
res.write('<html><head><title>Example</title></head><body>');
res.write('<form enctype="application/x-www-form-urlencoded" action="/formhandler" method="post">');
var qs = require('querystring');
var query = "foo[name]=bar&foo[bar]=foobar";
var res = qs.parse(query);
console.log(res.foo.foo);
console.log(res['foo']['bar']);
var printMemoryUsage = function() {
var spawn = require('child_process').spawn,
getconf = spawn('getconf', ['PAGESIZE']);
getconf.stdout.on('data', function (data) {
var pageSize = parseInt(data, 10);
console.log('Page size: ' + pageSize);
var procOutput = spawn('cat', ['/proc/' + process.pid + '/status']);
/*
* syslog-server.js
*
* @version 0.1.0
* @author Frank Grimm (http://frankgrimm.net)
*
*/
exports.getServer = function(bind_port, bind_ip, callback) {
/*
* syslog-messages.js
*
* Encoding and decoding of CentOS syslog messages.
*
* @version: 0.1.0
* @author Frank Grimm (http://frankgrimm.net)
*
*/
var Script = process.binding('evals').Script;
var stdin = process.openStdin();
stdin.setEncoding('utf8');
var scriptCode = '';
stdin.on('data', function (chunk) {
scriptCode += chunk;
});
stdin.on('end', function () {
Script.runInThisContext(scriptCode);
test-client-connect.js test-client-query.js
[root@speicher node-mysql]# node test/system/test-client-connect.js
{ length: 52
, received: 52
, number: 0
, type: 0
, protocolVersion: 10
, serverVersion: '5.0.45'
, threadId: 132500
, scrambleBuffer: <Buffer 66 3c 37 3d 6c 2b 2f 47 59 6b 2e 41 3c 48 35 70 56 3f 77 5b>