Run a Fastify application with one command!
Command line tools for Fastify. Generate, write, and run an application with one single command!
npm install fastify-cli --globalfastify-cli offers a single command line interface for your Fastify
project:
$ fastifyWhich will print a help message:
…You can start any Fastify plugin with:
$ fastify start plugin.jsA plugin can be as simple as:
// plugin.js
module.exports = function (fastify, options, next) {
fastify.get('/', function (req, reply) {
reply.send({ hello: 'world' })
})
next()
}If you are using Node 8+, you can use Promises or async functions too:
// async-await-plugin.js
module.exports = async function (fastify, options) {
fastify.get('/', async function (req, reply) {
return { hello: 'world' }
})
}For a list of available flags for fastify start see the help: fastify help start.
If you want to use custom options for the server creation, just export an options object with your route and run the cli command with the --options flag.
These options also get passed to your plugin via the options argument.
// 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'
}
}And if you are using EcmaScript Module format:
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
}If you want to use custom options for your plugin, just add them after the -- terminator. If used in conjunction with the --options argument, the CLI
arguments take precedence.
// plugin.js
module.exports = function (fastify, options, next) {
if (option.one) {
//...
}
//...
next()
}$ fastify start plugin.js -- --oneModules in EcmaScript Module format can be used on Node.js >= 14 or >= 12.17.0 but = 14 and the nearest parent package.json has "type": "module"
(more info here).
If your package.json does not have "type": "module", use .mjs for the extension (plugin.mjs in the above example).
You can pass the following options via CLI arguments. You can also use --config or -c flag to pass a configuration file that exports all the properties listed below in camelCase convention. In case of collision (i.e., An argument existing in both the configuration file and as a command-line argument, the command-line argument is given the priority). Every option has a corresponding environment variable:
| Description | Short command | Full command | Environment variable |
|---|---|---|---|
| Path to configuration file that can be used to manage the options listed below | -c |
--config |
FASTIFY_CONFIG or CONFIG |
| Port to listen on (default to 3000) | -p |
--port |
FASTIFY_PORT or PORT |
| Address to listen on | -a |
--address |
FASTIFY_ADDRESS |
| Socket to listen on | -s |
--socket |
FASTIFY_SOCKET |
| Module to preload | -r |
--require |
FASTIFY_REQUIRE |
| ES Module to preload | -i |
--import |
FASTIFY_IMPORT |
| Log level (default to fatal) | -l |
--log-level |
FASTIFY_LOG_LEVEL |
| Path to logging configuration module to use | -L |
--logging-module |
FASTIFY_LOGGING_MODULE |
| Start Fastify app in debug mode with nodejs inspector | -d |
--debug |
FASTIFY_DEBUG |
| Set the inspector port (default: 9320) | -I |
--debug-port |
FASTIFY_DEBUG_PORT |
Set the inspector host to listen on (default: loopback address or 0.0.0.0 inside Docker or Kubernetes) |
--debug-host |
FASTIFY_DEBUG_HOST |
|
| Prints pretty logs | -P |
--pretty-logs |
FASTIFY_PRETTY_LOGS |
| Watch process.cwd() directory for changes, recursively; when that happens, the process will auto reload | -w |
--watch |
FASTIFY_WATCH |
Ignore changes to the specified files or directories when watch is enabled. (e.g. --ignore-watch='node_modules .git logs/error.log' ) |
--ignore-watch |
FASTIFY_IGNORE_WATCH |
|
Watch changes only into the specified files or directories when watch is enabled. (e.g. --follow-watch='plugins/' ) |
--follow-watch |
FASTIFY_FOLLOW_WATCH |
|
Prints events triggered by watch listener (useful to debug unexpected reload when using --watch ) |
-V |
--verbose-watch |
FASTIFY_VERBOSE_WATCH |
| Use custom options | -o |
--options |
FASTIFY_OPTIONS |
| Set the prefix | -x |
--prefix |
FASTIFY_PREFIX |
| Set the plugin timeout | -T |
--plugin-timeout |
FASTIFY_PLUGIN_TIMEOUT |
| Defines the maximum payload, in bytes, | |||
| that the server is allowed to accept | --body-limit |
FASTIFY_BODY_LIMIT |
|
| Set the maximum ms delay before forcefully closing pending requests after receiving SIGTERM or SIGINT signals; and uncaughtException or unhandledRejection errors (default: 500) | -g |
--close-grace-delay |
FASTIFY_CLOSE_GRACE_DELAY |
Set the boolean value for trustProxy (1st precedence) |
--trust-proxy-enabled |
FASTIFY_TRUST_PROXY_ENABLED |
|
Set the IP/CIDR value for trustProxy (2nd precedence) |
--trust-proxy-ips |
FASTIFY_TRUST_PROXY_IPS |
|
Set the nth hop value for trustProxy (3rd precedence) |
--trust-proxy-hop |
FASTIFY_TRUST_PROXY_HOP |
By default, fastify-cli loads environment variables from .env in your current working directory using Node.js's built-in process.loadEnvFile() (requires Node.js >= 20.6.0).
The default value for --plugin-timeout is 10 seconds.
By default,--ignore-watch flag is set to ignore node_modules build dist .git bower_components logs .swp .nyc_output files.
When deploying to a Docker container, and potentially other, containers, it is advisable to set a fastify address of 0.0.0.0 because these containers do not default to exposing mapped ports to localhost.
For containers built and run specifically by the Docker Daemon or inside a Kubernetes cluster, fastify-cli is able to detect that the server process is running within a container and the 0.0.0.0 listen address is set automatically.
Other containerization tools (eg. Buildah and Podman) are not detected automatically, so the 0.0.0.0 listen address must be set explicitly with either the --address flag or the FASTIFY_ADDRESS environment variable.
If Fastify is installed as a project dependency (with npm install --save fastify),
then fastify-cli will use that version of Fastify when running the server.
Otherwise, fastify-cli will use the version of Fastify included within fastify-cli.
If you would like to turn your application into a standalone executable,
just add the following server.js:
…fastify-cli can also help with generating some project scaffolding to
kickstart the development of your next Fastify application. To use it:
fastify generate cd yourappnpm installThe sample code offers you the following npm tasks:
npm start - starts the applicationnpm run dev - starts the application with
pino-pretty pretty logging
(not suitable for production)npm test - runs the testsnpm run lint - fixes files accordingly to linter rules, for templates generated with --standardlintYou will find three different folders:
plugins: the folder where you will place all your custom pluginsroutes: the folder where you will declare all your endpointstest: the folder where you will declare all your testFinally, there will be an app.js file, which is your entry point.
It is a standard Fastify plugin and you will not need to add the listen method to run the server, just run it with one of the scripts above.
If the target directory exists fastify generate will fail unless the target directory is ., as in the current directory.
If the target directory is the current directory (.) and it already contains a package.json file, fastify generate will fail. This can
be overridden with the --integrate flag:
fastify generate . --integrate
This will add or alter the main, scripts, dependencies, and devDependencies fields on the package.json. In cases of file name collisions
for any files being added, the file will be overwritten with the new file added by fastify generate. If there is an existing app.js in this scenario,
it will be overwritten. Use the --integrate flag with care.
| Description | Full command |
|---|---|
| To generate ESM based JavaScript template | --esm |
| Use the TypeScript template | --lang=ts, --lang=typescript |
Overwrite it when the target directory is the current directory (.) |
--integrate |
| For |
No open issues yet, or sync has not completed.