create-fastify-app
![Greenkeeper badge](https://badges.greenkeeper.io/davidedantonio/create-fastify-app.svg)
Create Fastify App is an utility that help you to generate or add plugin to your Fastify project. This mean that you can easily:
- Generate a Fastify Project, also from a given swagger file.
- Generate a service skeleton in existing Fastify Project.
- Add Cors plugin in existing Fastify Project.
- Add MongoDB plugin in existing Fastify Project.
- Add Redis plugin in existing Fastify Project.
Install
If you want to use create-fastify-app
you must first install globally on your machine
npm install -g create-fastify-app
Usage
Generate Fastify projects and utilities:
create-fastify-app [command] <options>
Command
generate:project Generate a ready to use Fastify project
generate:service Generate service skeleton source in given project
add:mongo Add MongoDB plugin in given project folder
add:mysql Add MySQL plugin in given project folder
add:cors Add CORS plugin in given project folder
add:redis Add Redis plugin in given project folder
Options
-d, --directory
Fastify project folder
-h, --help
Show this help message
Except for generate:project
command the others work on an existent project created with create-fastify-app
.
generate:project
Generate a new Fastify project run following command
create-fastify-app generate:project -d <project-folder>
If -d
, or --directory
, option is omitted the new project will be created in curret folder. At this point further information will be asked:
- Application Name: the application name.
- Description: a short description non how the application do.
- Author: the application author.
- Email: the application email author.
- Version: semver version.
- Keywords: a list of comma separated keywords.
- License: the application license.
- Swagger File: a swagger file to start from (YAML or JSON).
After providing these information the entire application skeleton will be created for you. Simply run
cd /your/wonderful/application
npm install
npm run dev
Project structure
By default create-fastify-app
generate a project structured in this way:
/your/wonderful/application
├── README.md
├── app
│ ├── app.js
│ ├── plugins
│ │ ├── README.md
│ │ └── support.js
│ └── services
│ ├── README.md
│ ├── hello
│ │ └── index.js
│ └── root.js
├── args.js
├── help
│ └── start.txt
├── package.json
├── server.js
└── test
├── helper.js
├── plugins
│ └── support.test.js
└── services
├── example.test.js
└── root.test.js
The app
folder contains all you need to develop your application. In particular, she contains the following directories:
plugins
: here you can add all your plugins you need into you application.services
: here you can develop all the endpoint you need for your application, or the generated endpoint if you give a swagger file at project creation.test
: here you can declare all your test.
The package.json
file comes with three predefined npm task:
"scripts": {
"test": "tap test/**/*.test.js",
"start": "node server.js",
"dev": "node server.js -l info -P"
}
npm test
: runs the testnpm start
: start your applicationnpm run dev
: start your application with pino-colada
pretty logging
Options
You can pass the following options via command line with node server.js <options>
. Every options has the corresponding environment variable:
Description | Short command | Full command | Environment variable |
---|
The application file to start | -f | --file | FASTIFY_FILE |
Port to listen on (default to 3000) | -p | --port | FASTIFY_PORT or PORT |
Address to listen on | -a | --address | FASTIFY_ADDRESS |
Log level (default to fatal) | -l | --log-level | FASTIFY_LOG_LEVEL |
Prints pretty logs | -P | --pretty-logs | FASTIFY_PRETTY_LOGS |
Use custom options | -o | --options | FASTIFY_OPTIONS |
Set the prefix | -r | --prefix | FASTIFY_PREFIX |
Set the plugin timeout | -T | --plugin-timeout | FASTIFY_PLUGIN_TIMEOUT |
Defines the maximum payload, in bytes, the server is allowed to accept | | --body-limit | FASTIFY_BODY_LIMIT |
Generate a project from a swagger file
When you generate a new project you can give in input a swagger file. In an api driven project this feature can be very important because create-fastify-app
will generate all your project endpoints for you. You will only have to worry about the development of the various endpoints.
If you give in input the Petstore swagger file given as example on Swagger.io, you can see that create-fastify-app
automatically generate a project with fastify-swagger
already configured and ready to use in your project. If you take a look at /documentation
endpoint you'll find something like that:
![swagger-example](https://github.com/davidedantonio/create-fastify-app/raw/HEAD/./swagger.png)
In your app/services
folder you'll find your endpoints folder
./app/services
├── README.md
├── pet
│ ├── index.js
│ └── routes.schema.js
├── root.js
├── store
│ ├── index.js
│ └── routes.schema.js
└── user
├── index.js
└── routes.schema.js
generate:service
This command allow you to generate a new service skeleton for your api. You simply run
create-fastify-app generate:service -d <project-folder>
And give some information such as:
- Service Name: The name of the service you want to generate
- Methods to implement: GET, POST, PUT, DELETE etc.
- Api Prefix: The prefix url (for example
/api
)
Moreover the command generate for you a small set of test foreach methods.
add:mongo
If you want to easily add the fastify-mongodb
plugin to your application this command is all you need. Just simply run
create-fastify-app add:mongo -d <project-folder>
And give some information such as:
- MongoDB Host: your MongoDB host.
- MongoDB Port: your MongoDB port.
- MongoDB Collection: your default collection.
- MongoDB User: your MongoDB user.
- MongoDB Password: your MongoDB password.
At this point the command add the fastify-mongodb
to you application with the given information for your MongoDB connection.
add:mysql
If you want to easily add the fastify-mysql
plugin to your application this command is all you need. Just simply run
create-fastify-app add:mysql -d <project-folder>
And give some information such as:
- MySQL Host: your MySQL host.
- MySQL Port: your MySQL port.
- MySQL Database: your default database.
- MySQL User: your MySQL user.
- Mysql Password: your MySQL password.
At this point the command add the fastify-mysql
to you application with the given information for your MySQL connection.
add:redis
If you want to easily add the fastify-redis
plugin just simply run
create-fastify-app add:redis -d <project-folder>
And give some information such as:
- Redis Host: your redis host
- Redis Port: your redis port
- Redis Password: your redis password, if you have one.
- Redis Index: your redis index
exactly as the add:mongo
and add:mysql
command add:redis
add the plugin into your application plugins folder with the given information for your Redis connection.
add:cors
If you want to easily add the fastify-cors
plugin just simply run
create-fastify-app add:cors -d <project-folder>
And give some information such as:
- Method: at least one of DELETE, PATCH, POST, PUT, HEAD, OPTIONS
after the choises fastify-cors
plugin will be added in your application.
Contributing
If you feel you can help in any way, be it with examples, extra testing, or new features please open a pull request or open an issue.
The code follows the Standard code style.
![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)
Acknowledgements
It is inspired by the fastify-cli project. Some part have been extracted from it.
This project is kindly sponsored by Webeetle s.r.l.
License
Licensed under MIT.