view xml/en/docs/http/ngx_http_js_module.xml @ 2245:87a0e2c73a25

Refactored njs documentation.
author Yaroslav Zhuravlev <yar@nginx.com>
date Mon, 24 Sep 2018 19:10:29 +0300
parents 467aef18bf12
children 32ba43abf9cd
line wrap: on
line source

<?xml version="1.0"?>

<!--
  Copyright (C) Nginx, Inc.
  -->

<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">

<module name="Module ngx_http_js_module"
        link="/en/docs/http/ngx_http_js_module.html"
        lang="en"
        rev="17">

<section id="summary">

<para>
The <literal>ngx_http_js_module</literal> module is used to implement
location and variable handlers
in <link doc="../njs/index.xml">njs</link> —
a subset of the JavaScript language.
</para>

<para>
This module is not built by default.
Download and install instructions are available
<link doc="../njs/install.xml">here</link>.
</para>

</section>


<section id="example" name="Example Configuration">

<para>
<example>
load_module modules/ngx_http_js_module.so;
...

http {
    js_include http.js;

    js_set $foo     foo;
    js_set $summary summary;

    server {
        listen 8000;

        location / {
            add_header X-Foo $foo;
            js_content baz;
        }

        location = /summary {
            return 200 $summary;
        }

        location = /hello {
            js_content hello;
        }
    }
}
</example>
</para>

<para>
The <path>http.js</path> file:
<example>
function foo(r) {
    r.log("hello from foo() handler");
    return "foo";
}

function summary(r) {
    var a, s, h;

    s = "JS summary\n\n";

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

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

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

    return s;
}

function baz(r) {
    r.status = 200;
    r.headersOut.foo = 1234;
    r.headersOut['Content-Type'] = "text/plain; charset=utf-8";
    r.headersOut['Content-Length'] = 15;
    r.sendHeader();
    r.send("nginx");
    r.send("java");
    r.send("script");

    r.finish();
}

function hello(r) {
    r.return(200, "Hello world!");
}
</example>
</para>

</section>


<section id="directives" name="Directives">

<directive name="js_content">
<syntax><value>function</value></syntax>
<default/>
<context>location</context>
<context>limit_except</context>

<para>
Sets an njs function as a location content handler.
</para>

</directive>


<directive name="js_include">
<syntax><value>file</value></syntax>
<default/>
<context>http</context>

<para>
Specifies a file that implements location and variable handlers in njs.
</para>

</directive>


<directive name="js_set">
<syntax>
<value>$variable</value> <value>function</value></syntax>
<default/>
<context>http</context>

<para>
Sets an njs function for the specified variable.
</para>

</directive>

</section>


<section id="arguments" name="Request Argument">

<para>
Each HTTP njs handler receives one argument, a request
<link doc="../njs/njs_api.xml" id="http_request">object</link>.
</para>

</section>

</module>