Skip to content

Commit

Permalink
docs(readme): update example (#96)
Browse files Browse the repository at this point in the history
* fixes #90

* fix

* better wording

---------

Co-authored-by: derammo <[email protected]>
  • Loading branch information
Uzlopak and derammo authored Dec 6, 2023
1 parent 8d9c256 commit 0a85431
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![NPM version](https://img.shields.io/npm/v/@fastify/routes.svg?style=flat)](https://www.npmjs.com/package/@fastify/routes)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)

This plugin decorates a Fastify instance with `routes`, which is a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) of registered routes. Note that you have to register this plugin before registering any routes so that it can collect all of them.
This plugin decorates a Fastify instance with `routes`, which is a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) of registered routes. Note that you have to await the registration of this plugin before registering any routes so that @fastify/routes can collect them.

## Data Structure

Expand Down Expand Up @@ -40,36 +40,37 @@ The `fastify.routes` Map has a key for each path any route has been registered,
## Example

```js
const fastify = require('fastify')()
const fastify = require("fastify")();

fastify.register(require('@fastify/routes'))
(async () => {
await fastify.register(require("@fastify/routes"));
fastify.get("/hello", {}, (request, reply) => {
reply.send({ hello: "world" });
});

fastify.get('/hello', {}, (request, reply) => {
reply.send({ hello: 'world' })
})

fastify.listen({ port: 3000 }, (err, address) => {
if (err) {
console.error(err)
return
}
console.log(fastify.routes)
/* will output a Map with entries:
{
'/hello': [
{
method: 'GET',
url: '/hello',
schema: Object,
handler: <Function>,
prefix: <String>,
logLevel: <String>,
bodyLimit: <Number>
}
]
}
*/
})
fastify.listen({ port: 3000 }, (err, address) => {
if (err) {
console.error(err);
return;
}
console.log(fastify.routes);
/* will output a Map with entries:
{
'/hello': [
{
method: 'GET',
url: '/hello',
schema: Object,
handler: <Function>,
prefix: <String>,
logLevel: <String>,
bodyLimit: <Number>
}
]
}
*/
});
})();
```

## License
Expand Down

0 comments on commit 0a85431

Please sign in to comment.