Mercurial > hg > nginx-site
view xml/en/docs/http/ngx_http_js_module.xml @ 2582:e162a71453b0
Improved String.prototype.toString() for ordinary strings in njs.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Tue, 11 Aug 2020 12:34:32 +0100 |
parents | 33468af2eaf7 |
children | b4991bbd64ee |
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="22"> <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> Download and install instructions are available <link doc="../njs/install.xml">here</link>. </para> </section> <section id="example" name="Example Configuration"> <para> The example works since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>. <example> http { js_import http.js; js_set $foo http.foo; js_set $summary http.summary; server { listen 8000; location / { add_header X-Foo $foo; js_content http.baz; } location = /summary { return 200 $summary; } location = /hello { js_content http.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!"); } export default {foo, summary, baz, hello}; </example> </para> </section> <section id="directives" name="Directives"> <directive name="js_content"> <syntax><value>function</value> | <value>module.function</value></syntax> <default/> <context>location</context> <context>limit_except</context> <para> Sets an njs function as a location content handler. Since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>, a module function can be referenced. </para> </directive> <directive name="js_import"> <syntax><value>module.js</value> | <value>export_name from module.js</value></syntax> <default/> <context>http</context> <appeared-in>0.4.0</appeared-in> <para> Imports a module that implements location and variable handlers in njs. The <literal>export_name</literal> is used as a namespace to access module functions. If the <literal>export_name</literal> is not specified, the module name will be used as a namespace. <example> js_import http.js; </example> Here, the module name <literal>http</literal> is used as a namespace while accessing exports. If the imported module contains <literal>foo()</literal>, <literal>http.foo</literal> is used to refer to it. </para> <para> Several <literal>js_import</literal> directives can be specified. </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: <example> nginx.conf: js_include http.js; location /version { js_content version; } http.js: function version(r) { r.return(200, njs.version); } </example> </para> <para> The directive is deprecated since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>, the <link id="js_import"/> directive should be used instead. </para> </directive> <directive name="js_path"> <syntax> <value>path</value></syntax> <default/> <context>http</context> <appeared-in>0.3.0</appeared-in> <para> Sets an additional path for njs modules. </para> </directive> <directive name="js_set"> <syntax> <value>$variable</value> <value>function</value> | <value>module.function</value></syntax> <default/> <context>http</context> <para> Sets an njs function for the specified variable. Since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>, a module function can be referenced. </para> </directive> </section> <section id="arguments" name="Request Argument"> <para> Each HTTP njs handler receives one argument, a request <link doc="../njs/reference.xml" id="http">object</link>. </para> </section> </module>