https://github.com/metcoder95/fastify-get-head
Fastify plugin to set a HEAD route handler for each GET handler previously registered
https://github.com/metcoder95/fastify-get-head
Last synced: 3 months ago
JSON representation
Fastify plugin to set a HEAD route handler for each GET handler previously registered
- Host: GitHub
- URL: https://github.com/metcoder95/fastify-get-head
- Owner: metcoder95
- License: mit
- Created: 2020-11-04T21:33:58.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-08-25T10:50:20.000Z (almost 2 years ago)
- Last Synced: 2024-10-29T21:04:17.222Z (8 months ago)
- Language: JavaScript
- Size: 30.3 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-get-head


[](https://standardjs.com)A plugin for [Fastify](http://fastify.io/) that adds support for setting a `HEAD` route for each `GET` one previously registered.
This plugin's works via Fastify's `onRoute` hook. When a new route is registered, the plugin will try to set a new `HEAD` route if the registered one is for a `GET` method and is not ignored by the `ignorePaths` option.
**Note**: `fastify-get-head` only supports Fastify@>=3.8 <= 3.9
## Example
```js
const fastify = require('fastify')();fastify.register(require('fastify-get-head'), {
ignorePaths: ['/api/ignore', /\/api\/ignore\/too/], // For ignoring specific paths
});fastify.get('/', (req, reply) => {
reply.status(200).send({ hello: 'world' });
});// The plugin will create a new HEAD route where just the headers will be sent
// Same as doing:/**
* fastify.head('/', (req, reply) => {
* reply.headers({
* ['content-length']: Buffer.from(JSON.stringify({ hello: 'world' })).byteLength
* ['content-type']: 'application/json'
* });
* reply.status(200).send(null);
* });
```## Options
### ignorePaths
You're able to use either `string` and `regex` or even the combination of both with the use of an array. This to choose which routes you want to ignore. **Remember that only `GET` routes are taking into consideration**.
Example:
```javascript
fastify.register(require('fastify-get-head'), {
ignorePaths: '/api/ignore', // will ignore just `/api/ignore` path
});fastify.register(require('fastify-get-head'), {
ignorePaths: /\/api\/regex/, // this works as well
});fastify.register(require('fastify-get-head'), {
ignorePaths: ['/api/ignore', '/api/ignore/string'], // also works
});fastify.register(require('fastify-get-head'), {
ignorePaths: ['/api/ignore', /\/api\/regex/], // this works as well!
});
```## License
[MIT License](https://github.com/MetCoder95/fastify-get-head/blob/main/LICENSE)