Archive for October, 2011

Node.js and receiving POST-data from HTTP

Saturday, October 22nd, 2011

I have built an application that needed to communicate with another Linux server. It was very simple commands that my nodeserver should receive.

To get the POST-variables in the in-built HTTP-server from NodeJS you can use this method:

function onRequest(request, response) {
  var pathname = url.parse(request.url).pathname;
  console.log("Request for " + pathname + " received.");
  response.writeHead(200, {"Content-Type": "text/plain"});
 
  post_handler(request, function(request_data)
   {
	console.log(sys.inspect(request_data));
	response.end();
   });
}
function post_handler(request, callback)
{
    var _REQUEST = { };
    var _CONTENT = '';
 
    if (request.method == 'POST')
    {
        request.addListener('data', function(chunk)
	{
	    _CONTENT+= chunk;
	});
 
	request.addListener('end', function()
	{
        _REQUEST = querystring.parse(_CONTENT);
	    callback(_REQUEST);
	});
    };
};
 
http.createServer(onRequest).listen(8888);

Enable .bashrc on OSX Snow Leopard (and Lion)

Wednesday, October 19th, 2011

Unfortunately OSX doesn’t use .bashrc per default, but it is easy to enable it. To do it, open up the file /etc/profile with root privileges like this:

sudo nano /etc/profile

And add the following line:

[ -r $HOME/.bashrc ] && source $HOME/.bashrc

[ -r $HOME/.bashrc ] && source $HOME/.bashrc

Save, and now your .bashrc file should work like you expect.

Downloading files from SSH over an unstable connection

Sunday, October 9th, 2011

As mentioned earlier I am doing a lot of traveling at the moment, which means that I am mostly on a mobile connection.

I needed to download a 100Mb file from my Linux server, and with scp the download would fail.

Instead I am now using this rsync command that would resume if the connection drops:
rsync –partial –progress –rsh=ssh root@rauw.dk:filename.exe filename.exe

Also, if you are using SSH on a slow connection, typing things make lag, you can use this command:

rlwrap -a ssh user@address

X11forwarding on Mac OS X

Wednesday, October 5th, 2011

I am currently sitting in a position where the only way to get internet is through a mobile connection. This means that large downloads often are not possible because of the connection drops.

I needed to download a 500 mb file from RapidShare, that does not allow resuming of download after a connection drop (at least not for non-paying users). My idea was to let my server download the file and to let me download it afterwards with resuming.

The only problem was: The captcha since my server isn’t running a graphical user interface.

I found a program called Plowshare, that can download from a lot of download-websites, and send the Captcha image to my computer through the X11 connection.