@@ -1545,35 +1545,61 @@ a custom constraint strategy with the same name.
1545
1545
#### printRoutes
1546
1546
<a id="print-routes"></a>
1547
1547
1548
- ` fastify .printRoutes ()` : Prints the representation of the internal radix tree
1549
- used by the router, useful for debugging. Alternatively, ` fastify .printRoutes ({
1550
- commonPrefix: false })` can be used to print the flattened routes tree.
1548
+ ` fastify .printRoutes ()` : Fastify router builds a tree of routes for each HTTP
1549
+ method. If you call the prettyPrint without specifying an HTTP method, it will
1550
+ merge all the trees into one and print it. The merged tree doesn't represent the
1551
+ internal router structure. **Don't use it for debugging.**
1551
1552
1552
1553
*Remember to call it inside or after a ` ready` call.*
1553
1554
1554
1555
` ` ` js
1555
1556
fastify .get (' /test' , () => {})
1556
1557
fastify .get (' /test/hello' , () => {})
1557
- fastify .get (' /hello/world' , () => {})
1558
- fastify .get (' /helicopter' , () => {})
1558
+ fastify .get (' /testing' , () => {})
1559
+ fastify .get (' /testing/:param' , () => {})
1560
+ fastify .put (' /update' , () => {})
1559
1561
1560
1562
fastify .ready (() => {
1561
1563
console .log (fastify .printRoutes ())
1562
1564
// └── /
1563
1565
// ├── test (GET)
1564
- // │ └── /hello (GET)
1565
- // └── hel
1566
- // ├── lo/world (GET)
1567
- // └── licopter (GET)
1566
+ // │ ├── /hello (GET)
1567
+ // │ └── ing (GET)
1568
+ // │ └── /
1569
+ // │ └── :param (GET)
1570
+ // └── update (PUT)
1571
+ })
1572
+ ` ` `
1568
1573
1569
- console .log (fastify .printRoutes ({ commonPrefix: false }))
1570
- // └── / (-)
1571
- // ├── test (GET)
1572
- // │ └── /hello (GET)
1573
- // ├── hello/world (GET)
1574
- // └── helicopter (GET)
1574
+ If you want to print the internal router tree, you should specify the ` method`
1575
+ param. Printed tree will represent the internal router structure.
1576
+ **You can use it for debugging.**
1575
1577
1576
- })
1578
+ ` ` ` js
1579
+ console .log (fastify .printRoutes ({ method: ' GET' }))
1580
+ // └── /
1581
+ // └── test (GET)
1582
+ // ├── /hello (GET)
1583
+ // └── ing (GET)
1584
+ // └── /
1585
+ // └── :param (GET)
1586
+
1587
+ console .log (fastify .printRoutes ({ method: ' PUT' }))
1588
+ // └── /
1589
+ // └── update (PUT)
1590
+ ` ` `
1591
+
1592
+ ` fastify .printRoutes ({ commonPrefix: false })` will print compressed trees. This
1593
+ might useful when you have a large number of routes with common prefixes.
1594
+ It doesn't represent the internal router structure. **Don't use it for debugging.**
1595
+
1596
+ ` ` ` js
1597
+ console .log (fastify .printRoutes ({ commonPrefix: false }))
1598
+ // ├── /test (GET)
1599
+ // │ ├── /hello (GET)
1600
+ // │ └── ing (GET)
1601
+ // │ └── /:param (GET)
1602
+ // └── /update (PUT)
1577
1603
` ` `
1578
1604
1579
1605
` fastify .printRoutes ({ includeMeta: (true | []) })` will display properties from
@@ -1583,26 +1609,51 @@ A shorthand option, `fastify.printRoutes({ includeHooks: true })` will include
1583
1609
all [hooks](./Hooks.md).
1584
1610
1585
1611
` ` ` js
1586
- console .log (fastify .printRoutes ({ includeHooks: true , includeMeta: [' metaProperty' ] }))
1612
+ fastify .get (' /test' , () => {})
1613
+ fastify .get (' /test/hello' , () => {})
1614
+
1615
+ const onTimeout = () => {}
1616
+
1617
+ fastify .addHook (' onRequest' , () => {})
1618
+ fastify .addHook (' onTimeout' , onTimeout)
1619
+
1620
+ console .log (fastify .printRoutes ({ includeHooks: true , includeMeta: [' errorHandler' ] }))
1587
1621
// └── /
1588
- // ├── test (GET)
1589
- // │ • (onRequest) ["anonymous()","namedFunction()"]
1590
- // │ • (metaProperty) "value"
1591
- // │ └── /hello (GET)
1592
- // └── hel
1593
- // ├── lo/world (GET)
1594
- // │ • (onTimeout) ["anonymous()"]
1595
- // └── licopter (GET)
1622
+ // └── test (GET)
1623
+ // • (onTimeout) ["onTimeout()"]
1624
+ // • (onRequest) ["anonymous()"]
1625
+ // • (errorHandler) "defaultErrorHandler()"
1626
+ // test (HEAD)
1627
+ // • (onTimeout) ["onTimeout()"]
1628
+ // • (onRequest) ["anonymous()"]
1629
+ // • (onSend) ["headRouteOnSendHandler()"]
1630
+ // • (errorHandler) "defaultErrorHandler()"
1631
+ // └── /hello (GET)
1632
+ // • (onTimeout) ["onTimeout()"]
1633
+ // • (onRequest) ["anonymous()"]
1634
+ // • (errorHandler) "defaultErrorHandler()"
1635
+ // /hello (HEAD)
1636
+ // • (onTimeout) ["onTimeout()"]
1637
+ // • (onRequest) ["anonymous()"]
1638
+ // • (onSend) ["headRouteOnSendHandler()"]
1639
+ // • (errorHandler) "defaultErrorHandler()"
1596
1640
1597
1641
console .log (fastify .printRoutes ({ includeHooks: true }))
1598
1642
// └── /
1599
- // ├── test (GET)
1600
- // │ • (onRequest) ["anonymous()","namedFunction()"]
1601
- // │ └── /hello (GET)
1602
- // └── hel
1603
- // ├── lo/world (GET)
1604
- // │ • (onTimeout) ["anonymous()"]
1605
- // └── licopter (GET)
1643
+ // └── test (GET)
1644
+ // • (onTimeout) ["onTimeout()"]
1645
+ // • (onRequest) ["anonymous()"]
1646
+ // test (HEAD)
1647
+ // • (onTimeout) ["onTimeout()"]
1648
+ // • (onRequest) ["anonymous()"]
1649
+ // • (onSend) ["headRouteOnSendHandler()"]
1650
+ // └── /hello (GET)
1651
+ // • (onTimeout) ["onTimeout()"]
1652
+ // • (onRequest) ["anonymous()"]
1653
+ // /hello (HEAD)
1654
+ // • (onTimeout) ["onTimeout()"]
1655
+ // • (onRequest) ["anonymous()"]
1656
+ // • (onSend) ["headRouteOnSendHandler()"]
1606
1657
` ` `
1607
1658
1608
1659
#### printPlugins
0 commit comments