Skip to content

metcoder95/fastify-get-head

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

fastify-get-head

npm Build Status JavaScript Style Guide

A plugin for Fastify 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

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:

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

About

Fastify plugin to set a HEAD route handler for each GET handler previously registered

Resources

License

Stars

Watchers

Forks

Packages

No packages published