### Start a Fastify Plugin Source: https://github.com/fastify/fastify-cli/blob/main/README.md Use the 'fastify start' command to run a Fastify plugin file. ```bash $ fastify start plugin.js ``` -------------------------------- ### Start plugin.js passing custom options Source: https://github.com/fastify/fastify-cli/blob/main/help/start.txt Example of starting a Fastify application defined in 'plugin.js' and passing custom options to the plugin. ```bash fastify start plugin.js -- --custom-plugin-option-1 --custom-plugin-option-2 ``` -------------------------------- ### Start Plugin with Fastify-cli Source: https://github.com/fastify/fastify-cli/blob/main/templates/readme/README.md Use the Fastify-cli to start your plugin. This command assumes the plugin is configured correctly for the CLI. ```bash fastify start __pluginFileName__ ``` -------------------------------- ### Install Plugin with npm Source: https://github.com/fastify/fastify-cli/blob/main/templates/readme/README.md Use npm to install the plugin. Ensure you have Node.js and npm installed. ```bash npm i __packageName__ ``` -------------------------------- ### Install Plugin with Yarn Source: https://github.com/fastify/fastify-cli/blob/main/templates/readme/README.md Use Yarn to install the plugin. Ensure you have Node.js and Yarn installed. ```bash yarn add __packageName__ ``` -------------------------------- ### Run production mode Source: https://github.com/fastify/fastify-cli/blob/main/templates/app-esm/README.md Starts the application in production mode. ```bash npm start ``` -------------------------------- ### Install fastify-cli globally Source: https://github.com/fastify/fastify-cli/blob/main/README.md Install the fastify-cli tool globally using npm. ```bash npm install fastify-cli --global ``` -------------------------------- ### Start plugin.js on port 8080 Source: https://github.com/fastify/fastify-cli/blob/main/help/start.txt Example of starting a Fastify application defined in 'plugin.js' and listening on port 8080. ```bash fastify start -p 8080 plugin.js ``` -------------------------------- ### Basic Fastify Plugin Example Source: https://github.com/fastify/fastify-cli/blob/main/README.md A simple Fastify plugin that defines a GET route for '/'. Requires a 'next' callback. ```javascript // plugin.js module.exports = function (fastify, options, next) { fastify.get('/', function (req, reply) { reply.send({ hello: 'world' }) }) next() } ``` -------------------------------- ### Run development mode Source: https://github.com/fastify/fastify-cli/blob/main/templates/app-esm/README.md Starts the application in development mode, accessible at http://localhost:3000. ```bash npm run dev ``` -------------------------------- ### Register Plugin in Fastify Source: https://github.com/fastify/fastify-cli/blob/main/templates/readme/README.md Register the plugin within your Fastify application. This example shows basic registration and starting the server. ```javascript const fastify = require('fastify')() fastify.register(require('__packageName__')) fastify.listen({ port: 3000 }) ``` -------------------------------- ### Install __MY_PLUGIN__ with npm Source: https://github.com/fastify/fastify-cli/blob/main/templates/plugin/README.md Use this command to add the plugin to your project dependencies. ```bash npm i __MY_PLUGIN__ ``` -------------------------------- ### Async/Await Fastify Plugin Example Source: https://github.com/fastify/fastify-cli/blob/main/README.md A Fastify plugin using async/await syntax for handling requests. Does not require a 'next' callback. ```javascript // async-await-plugin.js module.exports = async function (fastify, options) { fastify.get('/', async function (req, reply) { return { hello: 'world' } }) } ``` -------------------------------- ### Build and Test Fastify Application Source: https://github.com/fastify/fastify-cli/blob/main/README.md Use the `build` utility to load your Fastify application instance for testing without starting the server. This is useful for writing assertions against your application's endpoints. Ensure the `fastify-cli/helper` module is imported. ```javascript // load the utility helper functions const { build, listen } = require('fastify-cli/helper') // write a test const { test } = require('node:test') const assert = require('node:assert') test('test my application', async t => { const argv = ['app.js'] const app = await build(argv, { extraParam: 'foo', skipOverride: true // If you want your application to be registered with fastify-plugin }) t.after(() => app.close()) // test your application here: const res = await app.inject('/') assert.deepStrictEqual(res.json(), { hello: 'one' }) }) ``` -------------------------------- ### Configure Linter in package.json Source: https://github.com/fastify/fastify-cli/blob/main/README.md Example configuration for adding a linter to a Fastify project's package.json file. ```diff "devDependencies": { + "neostandard": "^0.11.9", } "scripts": { + "pretest": "eslint", "test": "node --test test/**/*.test.js", "start": "fastify start -l info app.js", "dev": "fastify start -l info -P app.js", + "lint": "eslint --fix" }, ``` -------------------------------- ### Register __MY_PLUGIN__ in Fastify Source: https://github.com/fastify/fastify-cli/blob/main/templates/plugin/README.md Require the plugin and register it with Fastify, optionally providing configuration options. Ensure Fastify is installed. ```javascript const fastify = require('fastify')() fastify.register(require('__MY_PLUGIN__'), { // put your options here }) fastify.listen({ port: 3000 }) ``` -------------------------------- ### ESM Fastify Plugin with Options Source: https://github.com/fastify/fastify-cli/blob/main/README.md An example of an ES Module (ESM) Fastify plugin with options. Supports trailing slash ignoring. ```javascript export default async function plugin (fastify, options) { // Both `/foo` and `/foo/` are registered fastify.get('/foo/', async function (req, reply) { return 'foo' }) } export const options = { ignoreTrailingSlash: true } ``` -------------------------------- ### Using Test Helpers Source: https://github.com/fastify/fastify-cli/blob/main/README.md Demonstrates how to use the `build` utility to load a Fastify application and perform basic assertions using the Node Test runner. ```APIDOC ## Test Helpers When you use `fastify-cli` to run your project, you need a way to load your application to write assertions. ### `build` Utility The `build` utility asynchronously builds your application and returns the `fastify` instance without calling the `listen` method. ### `listen` Utility The `listen` utility asynchronously starts your application and returns the `fastify` instance listening on the configured port. Both utilities accept the following parameters: - `args`: A string or array of strings representing arguments passed to the `fastify-cli` command. - `pluginOptions`: An object containing options provided to the started plugin (e.g., `app.js`). - `serverOptions`: An object containing additional options for the Fastify server, similar to the `--options` command-line argument. - `serverModule`: An optional parameter to provide an already imported main server plugin module. ### Request Example ```javascript // load the utility helper functions const { build, listen } = require('fastify-cli/helper') // write a test const { test } = require('node:test') const assert = require('node:assert') test('test my application', async t => { const argv = ['app.js'] const app = await build(argv, { extraParam: 'foo', skipOverride: true // If you want your application to be registered with fastify-plugin }) t.after(() => app.close()) // test your application here: const res = await app.inject('/') assert.deepStrictEqual(res.json(), { hello: 'one' }) }) ``` ### Response Example ```json { "hello": "one" } ``` ### Test with Logging Enabled To enable logging to the console, configure the logger to output to `stderr`. ### Request Example ```javascript const logger = { transport: { target: 'pino-pretty', options: { destination: 2, }, }, } const argv = ['app.js'] test('test my application with logging enabled', async t => { const app = await build(argv, {}, { logger }) t.after(() => app.close()) // test your application here: const res = await app.inject('/') assert.deepStrictEqual(res.json(), { hello: 'one' }) }) ``` ``` -------------------------------- ### Generate Fastify Application Scaffolding Source: https://github.com/fastify/fastify-cli/blob/main/README.md Initializes a new Fastify project structure in the specified directory. ```bash fastify generate ``` -------------------------------- ### Fastify CLI Help Message Source: https://github.com/fastify/fastify-cli/blob/main/README.md Displays the available commands and their descriptions for the fastify-cli. ```bash $ fastify ``` ```text Fastify command line interface, available commands are: * start start a server * eject turns your application into a standalone executable with a server.(js|ts) file being added * generate generate a new project * generate-plugin generate a new plugin project * generate-swagger generate Swagger/OpenAPI schema for a project using @fastify/swagger * readme generate a README.md for the plugin * print-routes prints the representation of the internal radix tree used by the router, useful for debugging. * print-plugins prints the representation of the internal plugin tree used by avvio, useful for debugging. * version the current fastify-cli version * help help about commands Launch 'fastify help [command]' to know more about the commands. The default command is start, you can hit fastify start plugin.js to start plugin.js. ``` -------------------------------- ### Create a standalone server.js Source: https://github.com/fastify/fastify-cli/blob/main/README.md Use this template to migrate your application away from fastify-cli into a standalone executable. ```javascript 'use strict' // Read the .env file. try { process.loadEnvFile() } catch {} // Require the framework const Fastify = require('fastify') // Require library to exit fastify process, gracefully (if possible) const closeWithGrace = require('close-with-grace') // Instantiate Fastify with some config const app = Fastify({ logger: true }) // Register your application as a normal plugin. const appService = require('./app.js') app.register(appService) // delay is the number of milliseconds for the graceful close to finish closeWithGrace({ delay: process.env.FASTIFY_CLOSE_GRACE_DELAY || 500 }, async function ({ signal, err, manual }) { if (err) { app.log.error(err) } await app.close() }) // Start listening. app.listen({ port: process.env.PORT || 3000 }, (err) => { if (err) { app.log.error(err) process.exit(1) } }) ``` -------------------------------- ### Fastify CLI Configuration Options Source: https://github.com/fastify/fastify-cli/blob/main/README.md A list of available CLI arguments, their corresponding environment variables, and their purpose for configuring the Fastify server. ```APIDOC ## Fastify CLI Configuration ### Description Configure the Fastify CLI using command-line arguments, a configuration file, or environment variables. Command-line arguments take priority over configuration file settings. ### Options - **--config (-c)** (string) - Path to configuration file. - **--port (-p)** (number) - Port to listen on (default: 3000). - **--address (-a)** (string) - Address to listen on. - **--socket (-s)** (string) - Socket to listen on. - **--require (-r)** (string) - Module to preload. - **--import (-i)** (string) - ES Module to preload. - **--log-level (-l)** (string) - Log level (default: fatal). - **--logging-module (-L)** (string) - Path to logging configuration module. - **--debug (-d)** (boolean) - Start in debug mode. - **--debug-port (-I)** (number) - Inspector port (default: 9320). - **--debug-host** (string) - Inspector host. - **--pretty-logs (-P)** (boolean) - Print pretty logs. - **--watch (-w)** (boolean) - Watch directory for changes. - **--ignore-watch** (string) - Files/directories to ignore when watching. - **--follow-watch** (string) - Files/directories to watch. - **--verbose-watch (-V)** (boolean) - Print watch events. - **--options (-o)** (string) - Custom options. - **--prefix (-x)** (string) - Set the prefix. - **--plugin-timeout (-T)** (number) - Set plugin timeout. - **--body-limit** (number) - Maximum payload size in bytes. ``` -------------------------------- ### Run tests Source: https://github.com/fastify/fastify-cli/blob/main/templates/app-esm/README.md Executes the project test suite. ```bash npm run test ``` -------------------------------- ### Fastify Plugin Execution Source: https://github.com/fastify/fastify-cli/blob/main/templates/readme/README.md Instructions on how to register a plugin within a Fastify application and execute it using the Fastify CLI. ```APIDOC ## CLI Execution ### Description Start a Fastify plugin using the Fastify-cli tool. ### Command fastify start __pluginFileName__ ### Usage Example ```js const fastify = require('fastify')() fastify.register(require('__packageName__')) fastify.listen({ port: 3000 }) ``` ``` -------------------------------- ### Generate Plugin README Source: https://github.com/fastify/fastify-cli/blob/main/README.md Creates a README.md file for a plugin, automatically generating a package.json if one is missing. ```bash fastify readme ``` -------------------------------- ### Fastify Plugin with Server Options Source: https://github.com/fastify/fastify-cli/blob/main/README.md Export an 'options' object from your plugin file to pass custom server creation options. These options are also passed to the plugin via the 'options' argument. ```javascript // plugin.js module.exports = function (fastify, options, next) { fastify.get('/', function (req, reply) { reply.send({ hello: 'world' }) }) next() } module.exports.options = { https: { key: 'key', cert: 'cert' } } ``` -------------------------------- ### Generate Fastify Plugin Scaffolding Source: https://github.com/fastify/fastify-cli/blob/main/README.md Creates a boilerplate project structure specifically for developing Fastify plugins. ```bash fastify generate-plugin ``` -------------------------------- ### Integrate Fastify into Existing Directory Source: https://github.com/fastify/fastify-cli/blob/main/README.md Overrides the default failure behavior when generating files in a directory that already contains a package.json. ```bash fastify generate . --integrate ``` -------------------------------- ### Test Fastify Application with Logging Enabled Source: https://github.com/fastify/fastify-cli/blob/main/README.md Configure logging for your Fastify application during testing by providing a `logger` object with `pino-pretty` transport options. The `build` utility accepts this configuration to enable detailed log output to stderr. ```javascript const logger = { transport: { target: 'pino-pretty', options: { destination: 2, }, }, } const argv = ['app.js'] test('test my application with logging enabled', async t => { const app = await build(argv, {}, { logger }) t.after(() => app.close()) // test your application here: const res = await app.inject('/') assert.deepStrictEqual(res.json(), { hello: 'one' }) }) ``` -------------------------------- ### Pass Plugin-Specific Options via CLI Source: https://github.com/fastify/fastify-cli/blob/main/README.md Pass custom options to your plugin using the '--' terminator after the plugin file. CLI arguments take precedence over options defined in the plugin. ```javascript // plugin.js module.exports = function (fastify, options, next) { if (option.one) { //... } //... next() } ``` ```bash $ fastify start plugin.js -- --one ``` -------------------------------- ### Generate Swagger Schema Source: https://github.com/fastify/fastify-cli/blob/main/README.md Exports the Swagger/OpenAPI schema for a project using @fastify/swagger. ```bash fastify generate-swagger app.js ``` -------------------------------- ### ESM Fastify Plugin with Options (Node.js >= 14) Source: https://github.com/fastify/fastify-cli/blob/main/README.md An ES Module (ESM) Fastify plugin that returns options. Works with '.js' extension if package.json has 'type': 'module'. Use '.mjs' otherwise. ```javascript export default async function plugin (fastify, options) { fastify.get('/', async function (req, reply) { return options }) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.