Mercurial > hg > nginx-site
diff xml/en/docs/njs/njs_api.xml @ 2245:87a0e2c73a25
Refactored njs documentation.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Mon, 24 Sep 2018 19:10:29 +0300 |
parents | 467aef18bf12 |
children |
line wrap: on
line diff
--- a/xml/en/docs/njs/njs_api.xml Mon Sep 24 19:10:17 2018 +0300 +++ b/xml/en/docs/njs/njs_api.xml Mon Sep 24 19:10:29 2018 +0300 @@ -9,7 +9,7 @@ <article name="njs API" link="/en/docs/njs/njs_api.html" lang="en" - rev="7"> + rev="8"> <section id="summary"> @@ -1278,391 +1278,4 @@ </section> -<section id="example" name="Examples"> - - -<section id="example_urldecode" name="URL Decoding"> - -<para> -<example> -js_include urldecode.js; - -js_set $decoded_foo decoded_foo; -</example> -</para> - -<para> -The <path>urldecode.js</path> file: -<example> -function decoded_foo(r) { - return decodeURIComponent(r.args.foo); -} -</example> -</para> - -</section> - - -<section id="example_urlencode" name="URL Encoding"> - -<para> -<example> -js_include urlencode.js; - -js_set $encoded_foo encoded_foo; -... - -location / { - proxy_pass http://example.com?foo=$encoded_foo; -} -</example> -</para> - -<para> -The <path>urlencode.js</path> file: -<example> -function encoded_foo(r) { - return encodeURIComponent('foo & bar?'); -} -</example> -</para> - -</section> - - -<section id="example_internal_redirect" name="Internal Redirect"> - -<para> -<example> -js_include redirect.js; - -location /redirect { - js_content redirect; -} - -location @named { - return 200 named; -} -</example> -</para> - -<para> -The <path>redirect.js</path> file: -<example> -function redirect(r) { - r.internalRedirect('@named'); -} -</example> -</para> - -</section> - - -<section id="example_fast_response" name="Returning Fastest Response from Proxy"> - -<para> -<example> -js_include fastresponse.js; - -location /start { - js_content content; -} - -location /foo { - proxy_pass http://backend1; -} - -location /bar { - proxy_pass http://backend2; -} -</example> -</para> - -<para> -The <path>fastresponse.js</path> file: -<example> -function content(r) { - var n = 0; - - function done(res) { - if (n++ == 0) { - r.return(res.status, res.responseBody); - } - } - - r.subrequest('/foo', r.variables.args, done); - r.subrequest('/bar', r.variables.args, done); -} -</example> -</para> - -</section> - - -<section id="example_jwt" name="Creating HS JWT"> - -<para> -<example> -js_include hs_jwt.js; - -js_set $jwt jwt; -</example> -</para> - -<para> -The <path>hs_jwt.js</path> file: -<example> -function create_hs256_jwt(claims, key, valid) { - var header = { "typ" : "JWT", "alg" : "HS256", "exp" : Date.now() + valid }; - - var s = JSON.stringify(header).toBytes().toString('base64url') + '.' - + JSON.stringify(claims).toBytes().toString('base64url'); - - var h = require('crypto').createHmac('sha256', key); - - return s + '.' + h.update(s).digest().toString('base64url'); -} - -function jwt(r) { - var claims = { - "iss" : "nginx", - "sub" : "alice", - "foo" : 123, - "bar" : "qq", - "zyx" : false - }; - - return create_hs256_jwt(claims, 'foo', 600); -} -</example> -</para> - -</section> - - -<section id="example_subrequest" name="Accessing API from a Subrequest"> - -<para> -<example> -js_include subrequest.js; - -keyval_zone zone=foo:10m; -... - -location /keyval { - js_content set_keyval; -} - -location /version { - js_content version; -} - -location /api { - api write=on; -} -</example> -</para> - -<para> -The <path>subrequest.js</path> file: -<example> -function set_keyval(r) { - r.subrequest('/api/3/http/keyvals/foo', - { method: 'POST', - body: JSON.stringify({ foo: 789, bar: "ss dd 00" })}, - - function(res) { - if (res.status >= 300) { - r.return(res.status, res.responseBody); - return; - } - r.return(500); - }); -} - -function version(r) { - r.subrequest('/api/3/nginx', { method: 'GET' }, function(res) { - if (res.status != 200) { - r.return(res.status); - return; - } - - var json = JSON.parse(res.responseBody); - r.return(200, json.version); - }); -} -</example> -</para> - -</section> - - -<section id="example_secure_link" name="Creating secure_link Hash"> - -<para> -<example> -js_include hash.js; - -js_set $new_foo create_secure_link; -... - -location / { - secure_link $cookie_foo; - secure_link_md5 "$uri mykey"; - ... -} - -location @login { - add_header Set-Cookie "foo=$new_foo; Max-Age=60"; - return 302 /; -} -</example> -</para> - -<para> -The <path>hash.js</path> file: -<example> -function create_secure_link(r) { - return require('crypto').createHash('md5') - .update(r.uri).update(" mykey") - .digest('base64url'); -} -</example> -</para> - -</section> - - -<section id="example_legacy" name="Legacy Examples"> - - -<section id="example_legacy_stream" name="Stream Example"> - -<para> -Starting from njs <link doc="../njs/njs_changes.xml" id="njs0.2.4">0.2.4</link>, -stream configuration -<link doc="../stream/ngx_stream_js_module.xml" id="example">example</link> -has been changed. -For njs <link doc="../njs/njs_changes.xml" id="njs-0.2.3">0.2.3</link> -and earlier, use this configuration example: -<example> -load_module modules/ngx_stream_js_module.so; -... - -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> - -</section> - </article>