comparison 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
comparison
equal deleted inserted replaced
2244:467aef18bf12 2245:87a0e2c73a25
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> 7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
8 8
9 <article name="njs API" 9 <article name="njs API"
10 link="/en/docs/njs/njs_api.html" 10 link="/en/docs/njs/njs_api.html"
11 lang="en" 11 lang="en"
12 rev="7"> 12 rev="8">
13 13
14 <section id="summary"> 14 <section id="summary">
15 15
16 <para> 16 <para>
17 <link doc="index.xml">njs</link> provides objects, methods and properties 17 <link doc="index.xml">njs</link> provides objects, methods and properties
1276 </section> 1276 </section>
1277 1277
1278 </section> 1278 </section>
1279 1279
1280 1280
1281 <section id="example" name="Examples">
1282
1283
1284 <section id="example_urldecode" name="URL Decoding">
1285
1286 <para>
1287 <example>
1288 js_include urldecode.js;
1289
1290 js_set $decoded_foo decoded_foo;
1291 </example>
1292 </para>
1293
1294 <para>
1295 The <path>urldecode.js</path> file:
1296 <example>
1297 function decoded_foo(r) {
1298 return decodeURIComponent(r.args.foo);
1299 }
1300 </example>
1301 </para>
1302
1303 </section>
1304
1305
1306 <section id="example_urlencode" name="URL Encoding">
1307
1308 <para>
1309 <example>
1310 js_include urlencode.js;
1311
1312 js_set $encoded_foo encoded_foo;
1313 ...
1314
1315 location / {
1316 proxy_pass http://example.com?foo=$encoded_foo;
1317 }
1318 </example>
1319 </para>
1320
1321 <para>
1322 The <path>urlencode.js</path> file:
1323 <example>
1324 function encoded_foo(r) {
1325 return encodeURIComponent('foo &amp; bar?');
1326 }
1327 </example>
1328 </para>
1329
1330 </section>
1331
1332
1333 <section id="example_internal_redirect" name="Internal Redirect">
1334
1335 <para>
1336 <example>
1337 js_include redirect.js;
1338
1339 location /redirect {
1340 js_content redirect;
1341 }
1342
1343 location @named {
1344 return 200 named;
1345 }
1346 </example>
1347 </para>
1348
1349 <para>
1350 The <path>redirect.js</path> file:
1351 <example>
1352 function redirect(r) {
1353 r.internalRedirect('@named');
1354 }
1355 </example>
1356 </para>
1357
1358 </section>
1359
1360
1361 <section id="example_fast_response" name="Returning Fastest Response from Proxy">
1362
1363 <para>
1364 <example>
1365 js_include fastresponse.js;
1366
1367 location /start {
1368 js_content content;
1369 }
1370
1371 location /foo {
1372 proxy_pass http://backend1;
1373 }
1374
1375 location /bar {
1376 proxy_pass http://backend2;
1377 }
1378 </example>
1379 </para>
1380
1381 <para>
1382 The <path>fastresponse.js</path> file:
1383 <example>
1384 function content(r) {
1385 var n = 0;
1386
1387 function done(res) {
1388 if (n++ == 0) {
1389 r.return(res.status, res.responseBody);
1390 }
1391 }
1392
1393 r.subrequest('/foo', r.variables.args, done);
1394 r.subrequest('/bar', r.variables.args, done);
1395 }
1396 </example>
1397 </para>
1398
1399 </section>
1400
1401
1402 <section id="example_jwt" name="Creating HS JWT">
1403
1404 <para>
1405 <example>
1406 js_include hs_jwt.js;
1407
1408 js_set $jwt jwt;
1409 </example>
1410 </para>
1411
1412 <para>
1413 The <path>hs_jwt.js</path> file:
1414 <example>
1415 function create_hs256_jwt(claims, key, valid) {
1416 var header = { "typ" : "JWT", "alg" : "HS256", "exp" : Date.now() + valid };
1417
1418 var s = JSON.stringify(header).toBytes().toString('base64url') + '.'
1419 + JSON.stringify(claims).toBytes().toString('base64url');
1420
1421 var h = require('crypto').createHmac('sha256', key);
1422
1423 return s + '.' + h.update(s).digest().toString('base64url');
1424 }
1425
1426 function jwt(r) {
1427 var claims = {
1428 "iss" : "nginx",
1429 "sub" : "alice",
1430 "foo" : 123,
1431 "bar" : "qq",
1432 "zyx" : false
1433 };
1434
1435 return create_hs256_jwt(claims, 'foo', 600);
1436 }
1437 </example>
1438 </para>
1439
1440 </section>
1441
1442
1443 <section id="example_subrequest" name="Accessing API from a Subrequest">
1444
1445 <para>
1446 <example>
1447 js_include subrequest.js;
1448
1449 keyval_zone zone=foo:10m;
1450 ...
1451
1452 location /keyval {
1453 js_content set_keyval;
1454 }
1455
1456 location /version {
1457 js_content version;
1458 }
1459
1460 location /api {
1461 api write=on;
1462 }
1463 </example>
1464 </para>
1465
1466 <para>
1467 The <path>subrequest.js</path> file:
1468 <example>
1469 function set_keyval(r) {
1470 r.subrequest('/api/3/http/keyvals/foo',
1471 { method: 'POST',
1472 body: JSON.stringify({ foo: 789, bar: "ss dd 00" })},
1473
1474 function(res) {
1475 if (res.status >= 300) {
1476 r.return(res.status, res.responseBody);
1477 return;
1478 }
1479 r.return(500);
1480 });
1481 }
1482
1483 function version(r) {
1484 r.subrequest('/api/3/nginx', { method: 'GET' }, function(res) {
1485 if (res.status != 200) {
1486 r.return(res.status);
1487 return;
1488 }
1489
1490 var json = JSON.parse(res.responseBody);
1491 r.return(200, json.version);
1492 });
1493 }
1494 </example>
1495 </para>
1496
1497 </section>
1498
1499
1500 <section id="example_secure_link" name="Creating secure_link Hash">
1501
1502 <para>
1503 <example>
1504 js_include hash.js;
1505
1506 js_set $new_foo create_secure_link;
1507 ...
1508
1509 location / {
1510 secure_link $cookie_foo;
1511 secure_link_md5 "$uri mykey";
1512 ...
1513 }
1514
1515 location @login {
1516 add_header Set-Cookie "foo=$new_foo; Max-Age=60";
1517 return 302 /;
1518 }
1519 </example>
1520 </para>
1521
1522 <para>
1523 The <path>hash.js</path> file:
1524 <example>
1525 function create_secure_link(r) {
1526 return require('crypto').createHash('md5')
1527 .update(r.uri).update(" mykey")
1528 .digest('base64url');
1529 }
1530 </example>
1531 </para>
1532
1533 </section>
1534
1535
1536 <section id="example_legacy" name="Legacy Examples">
1537
1538
1539 <section id="example_legacy_stream" name="Stream Example">
1540
1541 <para>
1542 Starting from njs <link doc="../njs/njs_changes.xml" id="njs0.2.4">0.2.4</link>,
1543 stream configuration
1544 <link doc="../stream/ngx_stream_js_module.xml" id="example">example</link>
1545 has been changed.
1546 For njs <link doc="../njs/njs_changes.xml" id="njs-0.2.3">0.2.3</link>
1547 and earlier, use this configuration example:
1548 <example>
1549 load_module modules/ngx_stream_js_module.so;
1550 ...
1551
1552 stream {
1553 js_include stream.js;
1554
1555 js_set $foo foo;
1556 js_set $bar bar;
1557
1558 server {
1559 listen 12345;
1560
1561 js_preread qux;
1562 return $foo;
1563 }
1564
1565 server {
1566 listen 12346;
1567
1568 js_access xyz;
1569 proxy_pass 127.0.0.1:8000;
1570 js_filter baz;
1571 }
1572 }
1573
1574 http {
1575 server {
1576 listen 8000;
1577 location / {
1578 return 200 $http_foo\n;
1579 }
1580 }
1581 }
1582 </example>
1583 </para>
1584
1585 <para>
1586 The <path>stream.js</path> file:
1587 <example>
1588 var req = '';
1589 var matched = 0;
1590 var line = '';
1591
1592 function qux(s) {
1593 var n = s.buffer.indexOf('\n');
1594 if (n == -1) {
1595 return s.AGAIN;
1596 }
1597
1598 line = s.buffer.substr(0, n);
1599 }
1600
1601 function foo(s) {
1602 return line;
1603 }
1604
1605 function bar(s) {
1606 var v = s.variables;
1607 s.log("hello from bar() handler!");
1608 return "foo-var" + v.remote_port + "; pid=" + v.pid;
1609 }
1610
1611 // The filter processes one buffer per call.
1612 // The buffer is available in s.buffer both for
1613 // reading and writing. Called for both directions.
1614
1615 function baz(s) {
1616 if (s.fromUpstream || matched) {
1617 return;
1618 }
1619
1620 // Disable certain addresses.
1621
1622 if (s.remoteAddress.match('^192.*')) {
1623 return s.ERROR;
1624 }
1625
1626 // Read HTTP request line.
1627 // Collect bytes in 'req' until request
1628 // line is read. Clear current buffer to
1629 // disable output.
1630
1631 req = req + s.buffer;
1632 s.buffer = '';
1633
1634 var n = req.search('\n');
1635
1636 if (n != -1) {
1637 // Inject a new HTTP header.
1638 var rest = req.substr(n + 1);
1639 req = req.substr(0, n + 1);
1640
1641 var addr = s.remoteAddress;
1642
1643 s.log('req:' + req);
1644 s.log('rest:' + rest);
1645
1646 // Output the result and skip further
1647 // processing.
1648
1649 s.buffer = req + 'Foo: addr_' + addr + '\r\n' + rest;
1650 matched = 1;
1651 }
1652 }
1653
1654 function xyz(s) {
1655 if (s.remoteAddress.match('^192.*')) {
1656 return s.ABORT;
1657 }
1658 }
1659 </example>
1660 </para>
1661
1662 </section>
1663
1664 </section>
1665
1666 </section>
1667
1668 </article> 1281 </article>