Mercurial > hg > nginx-site
view xml/en/docs/http/ngx_http_js_module.xml @ 2187:ed905ab118c7
Updated HTTP njs module according to njs 0.2.2.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Tue, 19 Jun 2018 20:43:33 +0300 |
parents | cd4889fdcfa4 |
children | 523dc4cc8745 |
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="12"> <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(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(); } </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>