comparison xml/en/docs/stream/ngx_stream_js_module.xml @ 1840:15632fc2d548

Documented http and stream nginScript modules.
author Yaroslav Zhuravlev <yar@nginx.com>
date Fri, 18 Nov 2016 17:51:14 +0300
parents
children f56626ce9c40
comparison
equal deleted inserted replaced
1839:4035f9146bbf 1840:15632fc2d548
1 <?xml version="1.0"?>
2
3 <!--
4 Copyright (C) Nginx, Inc.
5 -->
6
7 <!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">
8
9 <module name="Module ngx_stream_js_module"
10 link="/en/docs/stream/ngx_stream_js_module.html"
11 lang="en"
12 rev="1">
13
14 <section id="summary">
15
16 <para>
17 The <literal>ngx_stream_js_module</literal> module is used to
18 implement handlers in JavaScript.
19 </para>
20
21 <para>
22 This module is not built by default, it should be compiled with
23 nginx JavaScript module using the
24 <literal>--add_module</literal> configuration parameter:
25 <example>
26 ./configure --add-module=<value>path-to-njs</value>/nginx
27 </example>
28 The <link url="http://hg.nginx.org/njs">repository</link>
29 with nginx JavaScript module can be cloned with the following command
30 (requires <link url="https://www.mercurial-scm.org">Mercurial</link> client):
31 <example>
32 hg clone http://hg.nginx.org/njs
33 </example>
34 This module can also be built as
35 <link doc="../ngx_core_module.xml" id="load_module">dynamic</link>:
36 <example>
37 ./configure --add-dynamic_module=<value>path-to-njs</value>/nginx
38 </example>
39 </para>
40
41 </section>
42
43
44 <section id="issues" name="Known Issues">
45
46 <para>
47 The module is experimental, caveat emptor applies.
48 </para>
49
50 </section>
51
52
53 <section id="example" name="Example Configuration">
54
55 <para>
56 <example>
57 stream {
58 js_include stream.js;
59
60 server {
61 listen 12345;
62
63 js_preread qux;
64
65 js_set $foo foo;
66 js_set $bar bar;
67
68 return foo;
69 }
70
71 server {
72 listen 12346;
73
74 js_access xyz;
75 proxy_pass 127.0.0.1:8000;
76 js_filter baz;
77 }
78 }
79
80 http {
81 server {
82 listen 8000;
83 location / {
84 return 200 $http_foo\n;
85 }
86 }
87 }
88 </example>
89 </para>
90
91 <para>
92 The <path>stream.js</path> file:
93 <example>
94 var req = '';
95 var matched = 0;
96 var line = '';
97
98 function qux(s) {
99 n = s.buffer.indexOf('\n');
100 if (n == -1) {
101 return s.AGAIN;
102 }
103
104 line = s.buffer.substr(0, n);
105 }
106
107 function foo(s) {
108 return line;
109 }
110
111 function bar(s) {
112 var v = s.variables;
113 s.log("hello from bar() handler!");
114 return "foo-var" + v.remote_port + "; pid=" + v.pid;
115 }
116
117 // The filter processes one buffer per call.
118 // The buffer is available in s.buffer both for
119 // reading and writing. Called for both directions.
120
121 function baz(s) {
122 if (s.fromUpstream || matched) {
123 return;
124 }
125
126 // Disable certain addresses.
127
128 if (s.remoteAddress.match('^192.*')) {
129 return s.ERROR;
130 }
131
132 // Read HTTP request line.
133 // Collect bytes in 'req' until request
134 // line is read. Clear current buffer to
135 // disable output.
136
137 req = req + s.buffer;
138 s.buffer = '';
139
140 n = req.search('\n');
141
142 if (n != -1) {
143 // Inject a new HTTP header.
144 var rest = req.substr(n + 1);
145 req = req.substr(0, n + 1);
146
147 addr = s.remoteAddress;
148
149 s.log('req:' + req);
150 s.log('rest:' + rest);
151
152 // Output the result and skip further
153 // processing.
154
155 s.buffer = req + 'Foo: addr_' + addr + '\r\n' + rest;
156 matched = 1;
157 }
158 }
159
160 function xyz(s) {
161 if (s.remoteAddress.match('^192.*')) {
162 return s.ABORT;
163 }
164 }
165 </example>
166 </para>
167
168 </section>
169
170
171 <section id="directives" name="Directives">
172
173 <directive name="js_access">
174 <syntax><value>function</value></syntax>
175 <default/>
176 <context>stream</context>
177 <context>server</context>
178
179 <para>
180 Sets a JavaScript function which will be called at the
181 <link doc="stream_processing.xml" id="access_phase">access</link> phase.
182 </para>
183
184 </directive>
185
186
187 <directive name="js_filter">
188 <syntax><value>function</value></syntax>
189 <default/>
190 <context>stream</context>
191 <context>server</context>
192
193 <para>
194 Sets a data filter.
195 </para>
196
197 </directive>
198
199
200 <directive name="js_include">
201 <syntax><value>file</value></syntax>
202 <default/>
203 <context>stream</context>
204 <context>server</context>
205
206 <para>
207 Specifies a file that implements server and variable handlers in JavaScript.
208 </para>
209
210 </directive>
211
212
213 <directive name="js_preread">
214 <syntax><value>function</value></syntax>
215 <default/>
216 <context>stream</context>
217 <context>server</context>
218
219 <para>
220 Sets a JavaScript function which will be called at the
221 <link doc="stream_processing.xml" id="preread_phase">preread</link> phase.
222 </para>
223
224 </directive>
225
226
227 <directive name="js_set">
228 <syntax>
229 <value>$variable</value> <value>function</value></syntax>
230 <default/>
231 <context>stream</context>
232 <context>server</context>
233
234 <para>
235 Sets a JavaScript function for the specified variable.
236 </para>
237
238 </directive>
239
240 </section>
241
242
243 <section id="properties" name="Session Object Properties">
244 <para>
245 Each stream JavaScript handler receives one argument, a stream session object.
246 </para>
247
248 <para>
249 The session object has the following properties:
250
251 <list type="tag">
252
253 <tag-name><literal>remoteAddress</literal></tag-name>
254 <tag-desc>
255 client address, read-only
256 </tag-desc>
257
258 <tag-name><literal>eof</literal></tag-name>
259 <tag-desc>
260 a boolean read-only property, true if the current buffer is the last buffer
261 </tag-desc>
262
263 <tag-name><literal>fromUpstream</literal></tag-name>
264 <tag-desc>
265 a boolean read-only property,
266 true if the current buffer is from the upstream server to the client
267 </tag-desc>
268
269 <tag-name><literal>buffer</literal></tag-name>
270 <tag-desc>
271 the current buffer, writable
272 </tag-desc>
273
274 <tag-name><literal>variables{}</literal></tag-name>
275 <tag-desc>
276 nginx variables object, read-only
277 </tag-desc>
278
279 <tag-name><literal>OK</literal></tag-name>
280 <tag-desc>
281 the <literal>OK</literal> return code
282 </tag-desc>
283
284 <tag-name><literal>DECLINED</literal></tag-name>
285 <tag-desc>
286 the <literal>DECLINED</literal> return code
287 </tag-desc>
288
289 <tag-name><literal>AGAIN</literal></tag-name>
290 <tag-desc>
291 the <literal>AGAIN</literal> return code
292 </tag-desc>
293
294 <tag-name><literal>ERROR</literal></tag-name>
295 <tag-desc>
296 the <literal>ERROR</literal> return code
297 </tag-desc>
298
299 <tag-name><literal>ABORT</literal></tag-name>
300 <tag-desc>
301 the <literal>ABORT</literal> return code
302 </tag-desc>
303 </list>
304 </para>
305
306 <para>
307 The session object has the following methods:
308
309 <list type="tag">
310
311 <tag-name><literal>log(<value>string</value>)</literal></tag-name>
312 <tag-desc>
313 writes a sent <value>string</value> to the error log
314 </tag-desc>
315 </list>
316 </para>
317
318 </section>
319
320 </module>