• Installing nginx 1.4.0 with SPDY support

    03 May 2013

    Since the version 1.4.0 nginx supports the draft 2 of SPDY protocol.

    But this SPDY module is not enabled by default and to enable it you need to entirely recompile nginx.

    Here's a simple guide to do that.

    Fist, be sure to have all needed packages (It can vary depending of your system, for information I'm doing this installation on Ubuntu 12.10)

    sudo apt-get update
    sudo apt-get install make gcc libpcre3-dev libssl-dev

    Then download the source code of the last stable version of nginx on their repository: http://hg.nginx.org/nginx/tags

    Currently the last stable version is the 1.4.0 (revision 7809529022b8);

    wget http://hg.nginx.org/nginx/archive/7809529022b8.tar.gz

    Decompress it:

    tar -xzf 7809529022b8.tar.gz
    cd nginx-7809529022b8

    Launch the configure command:

    ./auto/configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module

    (See the --with-http_spdy_module parameter at the end of the command)

    (If you already have nginx installed and want to know with which parameters it was compiled you can run the command nginx -V)

    Then if everything works fine, launch make and make install:

    make
    sudo make install

    Update your nginx configuration file

    server {
      listen 443 ssl spdy;
    
      ssl_certificate mycertificate.crt;
      ssl_certificate_key mycertificate.key;
    
      # ... my server configuration ...
    
    }

    Finally, (re)start nginx

    sudo service nginx restart

    And voilà! Your website should now run with SPDY.

  • Quick tip - Readable ternary operation

    28 April 2013

    You are certainly used to ternary operations in JavaScript, you know, those things you frequently use as a shortcut instead of a if statement:

    var sky = summer ? 'blue' : 'grey';

    Short, readable. Good.

    But what if we got more complex conditions ?

    var sky = spring ? 'lightblue' : summer ? 'blue' : fall ? 'lightgrey' : 'grey';

    Still short, but it start to be much less readable and difficult to understand at first sight.

    So, we can indent our code to keep this readable.

    var sky =
        spring ? 'lightblue' :
        summer ? 'blue'      :
        fall   ? 'lightgrey' :
        'grey'
    ;

    Haaaa, much better. We can easily see here our conditions and values while keeping the code quite short.

    Has a general rule:

    var [variable] =
        [condition 1] ? [value 1] :
        [condition 2] ? [value 2] :
        [fallback value]
    ;

    But, do you really need to do a complex ternary operation ? ;)

  • Chrome Logger for Node.js

    24 April 2013

    Chrome Logger is a Chrome extension that allows you to display your server side debugging messages in the Chrome console.

    So, I made a simple implementation for Node.js to help you to debug your Node.js application directly in Chrome.

    You can found node-chromelogger on GitHub.

    Juste make a

    npm install chromelogger

    like any other npm package.

    Then, use it in your application:

    var chromelogger = require('chromelogger');
    var http = require('http');
    
    var server = http.createServer();
    
    server.on('request', chromelogger.middleware);
    
    server.on('request', function(req, res) {
      res.chrome.log('Message from Node.js %s', process.version);
      res.end('Hello World');
    });
    
    server.listen(7357);

    Node Chrome Logger provide several logging methods on the ServerResponse (res) object:

    • res.chrome.log
    • res.chrome.warn
    • res.chrome.error
    • res.chrome.info
    • res.chrome.group
    • res.chrome.groupEnd
    • res.chrome.groupCollapse

    These methods matches the Console API of the Chrome Developer Tools.

    You can also use it as an ExpressJS middleware if you want (see Readme for more informations).

    The main limitation is it uses the HTTP Headers to send the informations to the client, so if you started to send the body you can't send more debug messages to the client and it will trigger an error.

    If you need a more advanced Node.js debug tool I suggest you to use the great Node Inspector package.