view xml/ru/docs/http/ngx_http_js_module.xml @ 2246:32ba43abf9cd

Renamed njs API, njs Changes.
author Yaroslav Zhuravlev <yar@nginx.com>
date Mon, 24 Sep 2018 19:24:04 +0300
parents 87a0e2c73a25
children 786e96c52c7a
line wrap: on
line source

<?xml version="1.0"?>

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

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

<module name="Модуль ngx_http_js_module"
        link="/ru/docs/http/ngx_http_js_module.html"
        lang="ru"
        rev="18">

<section id="summary">

<para>
Модуль <literal>ngx_http_js_module</literal> позволяет задавать
обработчики location и переменных
на <link doc="../njs/index.xml">njs</link> —
подмножестве языка JavaScript.
</para>

<para>
По умолчанию этот модуль не собирается.
Инструкция по сборке и установке доступны
<link doc="../njs/install.xml">здесь</link>.
</para>

</section>


<section id="example" name="Пример конфигурации">

<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>
Файл <path>http.js</path>:
<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="Директивы">

<directive name="js_content">
<syntax><value>функция</value></syntax>
<default/>
<context>location</context>
<context>limit_except</context>

<para>
Задаёт функцию njs в качестве обработчика содержимого location.
</para>

</directive>


<directive name="js_include">
<syntax><value>файл</value></syntax>
<default/>
<context>http</context>

<para>
Задаёт файл, позволяющий задавать обработчики location и переменных
на njs.
</para>

</directive>


<directive name="js_set">
<syntax>
<value>$переменная</value> <value>функция</value></syntax>
<default/>
<context>http</context>

<para>
Задаёт функцию njs для указанной переменной.
</para>

</directive>

</section>


<section id="arguments" name="Аргумент запроса">

<para>
Каждый HTTP-обработчик njs получает один аргумент,
<link doc="../njs/njs_api.xml" id="http_request">объект</link> запроса.
</para>

</section>

</module>