Skip to content

Instantly share code, notes, and snippets.

View alessioalex's full-sized avatar

Alexandru Vlăduţu alessioalex

View GitHub Profile
@alessioalex
alessioalex / gist:1342855
Created November 6, 2011 13:11
Node read stream
app.get('/logs/server', function (req, res) {
var readStream;
readStream = fs.createReadStream(basepath + "/logs/server.log");
readStream.setEncoding('utf8');
readStream.on('data', function(data) {
data = data.replace('\n', ',');
// res.header('Content-Type', 'text/plain');
res.send(data);
});
readStream.on('end', function(){});
@alessioalex
alessioalex / request post node.js
Created November 12, 2011 19:19
request post node.js
var request = require('request'), default_headers, site_root = 'http://localhost:3000';;
default_headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-us,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
// 'Connection': 'keep-alive',
'Cache-Control': 'max-age=0'
@alessioalex
alessioalex / gist:1374269
Created November 17, 2011 19:50
optimizable.node
// I have this code in my controller and it keeps repeating (but I wanna be DRY)
// so I thought of moving it in my model.... buuut
// the thing is it checks for errors and if so it displays them
// and as far as I know rendering content to the user from a model is considered a bad practice
Model.find_by_id(req.body.id, options, function(err, record) {
if (err) { render_json_internal_error(res, err); }
else { Model.check_something(api, req.session.user, callback); }
});
@alessioalex
alessioalex / gist:1376157
Created November 18, 2011 10:54
basic DOM Ready
(function(){
var DomReady = window.DomReady = {};
// Everything that has to do with properly supporting our document ready event. Brought over from the most awesome jQuery.
var userAgent = navigator.userAgent.toLowerCase();
// Figure out what browser is being used
var browser = {
@alessioalex
alessioalex / gist:1404012
Created November 29, 2011 08:31
Node command line read from input (hope I get this working)
/* Open the standard input stream and set the encoding to UTF-8 */
var stdin = process.openStdin();
stdin.setEncoding('utf8');
/* This is the callback for handing data events from standard input. */
inputHandler = function (response) {
/* Now that we're listening we either handle a prompt or handle a task
until the work queue is empty. */
if (response != 'exit') {
process.stdout.write(">> " + response);
@alessioalex
alessioalex / gist:1496429
Created December 19, 2011 10:08
HAProxy flash stuff
frontend flashpolicy 0.0.0.0:843
mode tcp
maxconn 20000
timeout client 86400000
default_backend flashpolicy_servers
backend flashpolicy_servers
mode tcp
balance roundrobin
timeout server 86400000
@alessioalex
alessioalex / stackoverflow_monitor.py
Created January 18, 2012 16:50 — forked from qiao/stackoverflow_monitor.py
Monitor for new questions on StackOverflow
#!/usr/bin/env python
# Monitor for new questions on StackOverflow
# Author: Xueqiao Xu <xueqiaoxu@gmail.com>
import time
import json
import gzip
import pprint
import urllib
import urllib2
@alessioalex
alessioalex / sendfile.js
Created March 13, 2012 17:49 — forked from pgriess/sendfile.js
Using sendfile(2) with NodeJS
var assert = require('assert');
var net = require('net');
var open = process.binding('fs').open;
var sendfile = process.binding('fs').sendfile;
if (process.argv.length < 4) {
console.error('usage: sendfile <port> <path>');
process.exit(1);
}
@alessioalex
alessioalex / .gitignore
Created April 7, 2012 15:58 — forked from bergie/.gitignore
Node.js email handling examples
config.json
reading-image.png
@alessioalex
alessioalex / gist:3306787
Created August 9, 2012 18:24 — forked from fxsjy/gist:3291755
Memcached in JavaScript based on Node.JS
//author: Sun, Junyi (weibo.com/treapdb)
//usage: node --nouse-idle-notification --expose-gc --max-old-space-size=8192 memcached.js
var config ={
port: 11211,
max_memory: 100 // default 100M bytes
}
var net = require('net');
var LRU = function (max) { // this LRU implementaion is based on https://github.com/chriso/lru