Mercurial > hg > nginx-site
view xml/en/docs/stream/ngx_stream_js_module.xml @ 2188:523dc4cc8745
Updated installation procedure in njs modules.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Tue, 19 Jun 2018 20:43:33 +0300 |
parents | cd4889fdcfa4 |
children | dfc49994218c |
line wrap: on
line source
<?xml version="1.0"?> <!-- Copyright (C) Nginx, Inc. --> <!DOCTYPE module SYSTEM "../../../../dtd/module.dtd"> <module name="Module ngx_stream_js_module" link="/en/docs/stream/ngx_stream_js_module.html" lang="en" rev="12"> <section id="summary"> <para> The <literal>ngx_stream_js_module</literal> module is used to implement handlers in <link doc="../njs_about.xml">njs</link> — a subset of the JavaScript language. </para> <para> This module is not built by default. Download and install instructions are available <link doc="../njs_about.xml" id="install">here</link>. </para> </section> <section id="example" name="Example Configuration"> <para> <example> stream { js_include stream.js; js_set $foo foo; js_set $bar bar; server { listen 12345; js_preread qux; return $foo; } server { listen 12346; js_access xyz; proxy_pass 127.0.0.1:8000; js_filter baz; } } http { server { listen 8000; location / { return 200 $http_foo\n; } } } </example> </para> <para> The <path>stream.js</path> file: <example> var req = ''; var matched = 0; var line = ''; function qux(s) { var n = s.buffer.indexOf('\n'); if (n == -1) { return s.AGAIN; } line = s.buffer.substr(0, n); } function foo(s) { return line; } function bar(s) { var v = s.variables; s.log("hello from bar() handler!"); return "foo-var" + v.remote_port + "; pid=" + v.pid; } // The filter processes one buffer per call. // The buffer is available in s.buffer both for // reading and writing. Called for both directions. function baz(s) { if (s.fromUpstream || matched) { return; } // Disable certain addresses. if (s.remoteAddress.match('^192.*')) { return s.ERROR; } // Read HTTP request line. // Collect bytes in 'req' until request // line is read. Clear current buffer to // disable output. req = req + s.buffer; s.buffer = ''; var n = req.search('\n'); if (n != -1) { // Inject a new HTTP header. var rest = req.substr(n + 1); req = req.substr(0, n + 1); var addr = s.remoteAddress; s.log('req:' + req); s.log('rest:' + rest); // Output the result and skip further // processing. s.buffer = req + 'Foo: addr_' + addr + '\r\n' + rest; matched = 1; } } function xyz(s) { if (s.remoteAddress.match('^192.*')) { return s.ABORT; } } </example> </para> </section> <section id="directives" name="Directives"> <directive name="js_access"> <syntax><value>function</value></syntax> <default/> <context>stream</context> <context>server</context> <para> Sets an njs function which will be called at the <link doc="stream_processing.xml" id="access_phase">access</link> phase. </para> </directive> <directive name="js_filter"> <syntax><value>function</value></syntax> <default/> <context>stream</context> <context>server</context> <para> Sets a data filter. </para> </directive> <directive name="js_include"> <syntax><value>file</value></syntax> <default/> <context>stream</context> <para> Specifies a file that implements server and variable handlers in njs. </para> </directive> <directive name="js_preread"> <syntax><value>function</value></syntax> <default/> <context>stream</context> <context>server</context> <para> Sets an njs function which will be called at the <link doc="stream_processing.xml" id="preread_phase">preread</link> phase. </para> </directive> <directive name="js_set"> <syntax> <value>$variable</value> <value>function</value></syntax> <default/> <context>stream</context> <para> Sets an njs function for the specified variable. </para> </directive> </section> <section id="properties" name="Session Object Properties"> <para> Each stream njs handler receives one argument, a stream session <link doc="../njs/njs_api.xml" id="stream_session">object</link>. </para> </section> </module>