view xml/en/docs/http/ngx_http_js_module.xml @ 2149:6df1a86a60b8

Added new njs HTTP properties and methods.
author Roman Arutyunyan <arut@nginx.com>
date Fri, 06 Apr 2018 20:34:26 +0300
parents ca7568f67dee
children cd4889fdcfa4
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="10">

<section id="summary">

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

<para>
This module is not built by default, it should be compiled with
the njs module using the
<literal>--add-module</literal> configuration parameter:
<example>
./configure --add-module=<value>path-to-njs</value>/nginx
</example>
The <link url="http://hg.nginx.org/njs">repository</link>
with the njs module can be cloned with the following command
(requires <link url="https://www.mercurial-scm.org">Mercurial</link> client):
<example>
hg clone http://hg.nginx.org/njs
</example>
This module can also be built as
<link doc="../ngx_core_module.xml" id="load_module">dynamic</link>:
<example>
./configure --add-dynamic-module=<value>path-to-njs</value>/nginx
</example>
</para>

</section>


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

<para>
<example>
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;
    }
}
</example>
</para>

<para>
The <path>http.js</path> file:
<example>
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();
}
</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 and Response Arguments">
<para>
Each HTTP njs handler receives two arguments, request and response.
</para>

<para>
The request object has the following properties:
<list type="tag">

<tag-name><literal>uri</literal></tag-name>
<tag-desc>
current URI in a request, read-only
</tag-desc>

<tag-name><literal>method</literal></tag-name>
<tag-desc>
request method, read-only
</tag-desc>

<tag-name><literal>httpVersion</literal></tag-name>
<tag-desc>
HTTP version, read-only
</tag-desc>

<tag-name><literal>remoteAddress</literal></tag-name>
<tag-desc>
client address, read-only
</tag-desc>

<tag-name><literal>headers{}</literal></tag-name>
<tag-desc>
request headers object, read-only.
<para>
For example, the <literal>Header-Name</literal> header
can be accessed with the syntax <literal>headers['Header-Name']</literal>
or <literal>headers.Header_name</literal>
</para>
</tag-desc>

<tag-name><literal>args{}</literal></tag-name>
<tag-desc>
request arguments object, read-only
</tag-desc>

<tag-name><literal>variables{}</literal></tag-name>
<tag-desc>
nginx variables object, read-only
</tag-desc>

<tag-name><literal>response</literal></tag-name>
<tag-desc>
the response object (0.2.0), read-only
</tag-desc>
</list>
</para>

<para>
The request object has the following methods:
<list type="tag">

<tag-name><literal>log(<value>string</value>)</literal></tag-name>
<tag-desc>
writes a <literal>string</literal> to the error log
on the <literal>info</literal> level of logging
</tag-desc>

<tag-name><literal>warn(<value>string</value>)</literal></tag-name>
<tag-desc>
writes a <literal>string</literal> to the error log
on the <literal>warning</literal> level of logging (0.2.0)
</tag-desc>

<tag-name><literal>error(<value>string</value>)</literal></tag-name>
<tag-desc>
writes a <literal>string</literal> to the error log
on the <literal>error</literal> level of logging (0.2.0)
</tag-desc>

<tag-name><literal>subrequest(<value>uri</value>[, <value>options</value>[,
<value>callback</value>]])</literal></tag-name>
<tag-desc>
creates a subrequest with the given <literal>uri</literal> and
<literal>options</literal>, and installs
an optional completion <literal>callback</literal> (0.2.0).

<para>
If <literal>options</literal> is a string, then it
holds the subrequest arguments string.
Otherwise <literal>options</literal> is expected to be
an object with the following keys:
<list type="tag">
<tag-name><literal>args</literal></tag-name>
<tag-desc>arguments string</tag-desc>

<tag-name><literal>body</literal></tag-name>
<tag-desc>request body</tag-desc>

<tag-name><literal>method</literal></tag-name>
<tag-desc>HTTP method</tag-desc>
</list>
</para>

<para>
The <literal>callback</literal> receives a response object
with the following properties:
<literal>uri</literal>, <literal>method</literal>,
<literal>status</literal>, <literal>contentType</literal>,
<literal>contentLength</literal>, <literal>headers</literal>,
<literal>args</literal>.
These properties have the same meaning as the request object properties.
Additionally, a reply object has the <literal>body</literal> property
holding the subrequest response body
and the <literal>parent</literal> property
referencing the parent request object.
</para>
</tag-desc>
</list>
</para>

<para>
The response object has the following properties:
<list type="tag">

<tag-name><literal>status</literal></tag-name>
<tag-desc>
response status, writable
</tag-desc>

<tag-name><literal>headers{}</literal></tag-name>
<tag-desc>
response headers object
</tag-desc>

<tag-name><literal>contentType</literal></tag-name>
<tag-desc>
the response <header>Content-Type</header> header field value, writable
</tag-desc>

<tag-name><literal>contentLength</literal></tag-name>
<tag-desc>
the response <header>Content-Length</header> header field value, writable
</tag-desc>
</list>
</para>

<para>
The response object has the following methods:
<list type="tag">

<tag-name><literal>sendHeader()</literal></tag-name>
<tag-desc>
sends the HTTP header to the client
</tag-desc>

<tag-name><literal>send(<value>string</value>)</literal></tag-name>
<tag-desc>
sends a part of the response body to the client
</tag-desc>

<tag-name><literal>finish()</literal></tag-name>
<tag-desc>
finishes sending a response to the client
</tag-desc>

<tag-name><literal>return(status[, string])</literal></tag-name>
<tag-desc>
sends
the entire response with the specified <literal>status</literal> to the client
(0.2.0)
<para>
It is possible to specify either a redirect URL
(for codes 301, 302, 303, 307, and 308)
or the response body text (for other codes) as the second argument.
</para>
</tag-desc>
</list>
</para>

</section>

</module>