
Configure nginx with HTTP and Stream JavaScript modules using the --add-module
option:

    ./configure --add-module=<path-to-njs>/nginx

Alternatively, you can build a dynamic version of the modules

    ./configure --add-dynamic-module=<path-to-njs>/nginx

and add the following lines to nginx.conf to load them

    load_module modules/ngx_http_js_module.so;
    load_module modules/ngx_stream_js_module.so;

Please report your experiences to the NGINX development mailing list
nginx-devel@nginx.org (http://mailman.nginx.org/mailman/listinfo/nginx-devel).


HTTP JavaScript module
----------------------

Each HTTP JavaScript handler receives two arguments - request and response.

    function foo(req, res) {
        ..
    }

The following properties are available:

req
    - uri
    - method
    - httpVersion
    - remoteAddress
    - headers{}
    - args{}
    - variables{}
    - log()

res
    - status
    - headers{}
    - contentType
    - contentLength
    - sendHeader()
    - send()
    - finish()


Example nginx.conf:

    # load dynamic HTTP JavaScript module
    load_module modules/ngx_http_js_module.so;

    worker_processes 1;
    pid logs/nginx.pid;

    events {
        worker_connections  256;
    }

    http {
        # include JavaScript file
        js_include http.js;

        server {
            listen 8000;

            location / {
                # create $foo variable and set JavaScript function foo()
                # from the included JavaScript file as its handler
                js_set $foo foo;

                add_header X-Foo $foo;

                # register JavaScript function bar() as content handler
                js_content baz;
            }

            location /summary {
                js_set $summary summary;

                return 200 $summary;
            }
        }
    }


http.js:

    function foo(req, res) {
        req.log("hello from foo() handler");
        return "foo";
    }

    function summary(req, res) {
        var a, s, h;

        s = "JS summary\n\n";

        s += "Method: " + req.method + "\n";
        s += "HTTP version: " + req.httpVersion + "\n";
        s += "Host: " + req.headers.host + "\n";
        s += "Remote Address: " + req.remoteAddress + "\n";
        s += "URI: " + req.uri + "\n";

        s += "Headers:\n";
        for (h in req.headers) {
            s += "  header '" + h + "' is '" + req.headers[h] + "'\n";
        }

        s += "Args:\n";
        for (a in req.args) {
            s += "  arg '" + a + "' is '" + req.args[a] + "'\n";
        }

        return s;
    }

    function baz(req, res) {
        res.headers.foo = 1234;
        res.status = 200;
        res.contentType = "text/plain; charset=utf-8";
        res.contentLength = 15;
        res.sendHeader();
        res.send("nginx");
        res.send("java");
        res.send("script");

        res.finish();
    }


Stream JavaScript module
------------------------

Each Stream JavaScript handler receives one argument - stream session object.

    function foo(s) {
        ..
    }

The following properties are available in the session object:

     - remoteAddress
     - variables{}
     - log()


Example nginx.conf:

    # load dynamic Stream JavaScript module
    load_module modules/ngx_stream_js_module.so;

    worker_processes 1;
    pid logs/nginx.pid;

    events {
        worker_connections  256;
    }

    stream {
        # include JavaScript file
        js_include stream.js;

        server {
            listen 8000;

            # create $foo and $bar variables and set JavaScript
            # functions foo() and bar() from the included JavaScript
            # file as their handlers
            js_set $foo foo;
            js_set $bar bar;

            return $foo-$bar;
        }
    }


stream.js:

    function foo(s) {
        s.log("hello from foo() handler!");
        return s.remoteAddress;
    }

    function bar(s) {
        var v = s.variables;
        s.log("hello from bar() handler!");
        return "foo-var" + v.remote_port + "; pid=" + v.pid;
    }


--
NGINX, Inc., http://nginx.com
