Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@andyburke/fs-fastify

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@andyburke/fs-fastify

Drive your fastify setup from the filesystem.

  • 1.1.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

fs-fastify

A module to drive your fastify setup via the filesystem.

Example

const fastify = require( 'fastify' )( {
	logger: true
} );
const fs_fastify = require( '@andyburke/fs-fastify' );

const routes = await fs_fastify();
for ( const route of routes ) {
	fastify.route( route );
}

fastify.listen( 3000, function ( err, address ) {
  if ( err ) {
    fastify.log.error( err );
    process.exit( 1 );
  }
  fastify.log.info( `server listening on ${address}` );
} );

This will load all files that match the required options and register them as routes in your fastify server.

For example, you could lay out your files thusly:

./api
	foo/
		:bar/
			GET.js
		:baz/
			POST.js
		zip/
			PATCH.js

This would create the following routes:

GET /foo/:bar
POST /foo/:baz
PATCH /foo/zip

Options

root = 'api'

Sets the root folder to find routes in. By default this is set to 'api'.

extensions = [ 'js', 'cjs' ]

Sets the extensions to search for.

methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS' ]

Sets the methods to look for in the file tree. Defaults to all normal HTTP methods.

Route Definitions

Your routes should export either a single function, eg:

module.exports = function ( request, reply ) {
	return {
		hello: "world"
	};
};

Or you can export a fastify route object, eg:

module.exports = {
	// you should omit the method, as it will be filled by fs-fastify based on
	// the filename
	// method: 'GET',

	// you should omit the url, as it will be filled by fs-fastify based on
	// the filename
	// url: '/',
	
	schema: {
		response: {
			200: {
				type: 'object',
				properties: {
					hello: { type: 'string' }
				}
			}
		}
	},

	handler: function (request, reply) {
		reply.send( { hello: 'world' } )
	}
};

Keywords

FAQs

Package last updated on 22 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc