view xml/en/docs/http/ngx_http_js_module.xml @ 2175:cd4889fdcfa4

Moved njs HTTP and Stream API to a separate page.
author Yaroslav Zhuravlev <yar@nginx.com>
date Tue, 05 Jun 2018 18:22:00 +0300
parents 6df1a86a60b8
children ed905ab118c7
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="11">

<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,
<link doc="../njs/njs_api.xml" id="http_request">request</link>
and <link doc="../njs/njs_api.xml" id="http_response">response</link>.
</para>

</section>

</module>