|
| 1 | +# TypeScript Fastify Backend Boilerplate |
| 2 | + |
| 3 | +Opinionated TypeScript based backend service boilerplate with Fastify. |
| 4 | + |
| 5 | +## Getting started |
| 6 | +```sh |
| 7 | +# clone this repo |
| 8 | +git clone https://github.com/Devtography/typescript-fastify-backend-boilerplate.git |
| 9 | +``` |
| 10 | +Or generate a new repo on GitHub using this template [here](https://github.com/Devtography/typescript-fastify-backend-boilerplate/generate) |
| 11 | + |
| 12 | +```json |
| 13 | +// edit the following fields in package.json for your own project |
| 14 | +{ |
| 15 | + "name": your-project-name, |
| 16 | + "version": whatever-you-like, |
| 17 | + "description": your-own-description, |
| 18 | + "author": who's-the-author?, |
| 19 | + "license": if-you-don't-want-to-use-MIT, |
| 20 | + "repository": type-and-link-of-your-repo, |
| 21 | + "keywords": keywords-of-your-project, |
| 22 | + "bugs": issue-page-of-your-repo, |
| 23 | + "homepage": homepage-of-your-repo |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +Then install all the `node_modules` needed by executing the following command: |
| 28 | +```sh |
| 29 | +cd folder-containing-the-cloned-boilerplate |
| 30 | +npm install --also-dev |
| 31 | +``` |
| 32 | + |
| 33 | +Manually create folders `src/models` & `src/utils` (folder structure shown |
| 34 | +below) as Git doesn't include empty folders. |
| 35 | + |
| 36 | +That's it. You're ready to rock. |
| 37 | + |
| 38 | +## Usage |
| 39 | +There're currently 4 NPM commands preconfigured in `package.json`, they are: |
| 40 | + |
| 41 | +`npm run build` - Removes everything from `dist/`, then compile your `.ts` files |
| 42 | +into `JavaScript` with `tsc`, and use `babel` to correct the module require |
| 43 | +paths. |
| 44 | + |
| 45 | +`npm run debug` - runs the `fastify` server with `ts-node-dev` with `NODE_ENV` |
| 46 | +set as `development` for debug. The `fastify` server will respawn on file saved |
| 47 | +under this mode. |
| 48 | + |
| 49 | +`npm run test` - runs your `Jest` tests under `tests/` with coverage reports. |
| 50 | + |
| 51 | +`npm run watch-test` - same as `npm run test` but execute on file saved |
| 52 | +automatically. |
| 53 | + |
| 54 | +For `vscode` users, 2 launch configs - `Debug` & `Jest tests` are included in |
| 55 | +this boilerplate. Use those launch configs to run your app / `Jest` tests if |
| 56 | +you need the breakpoints to work. |
| 57 | + |
| 58 | +## Folder structure |
| 59 | +``` |
| 60 | +typescript-fastify-backend-boilerplate/ |
| 61 | +| - .vscode/ |
| 62 | +| | - launch.json //- Preconfigured vscode debug settings |
| 63 | +| - node_modules/ |
| 64 | +| - dist/ //- Generated by `tsc` automatically |
| 65 | +| - src/ |
| 66 | +| | - apis/ //- Folder which `fastify-autoload` looks for |
| 67 | +| | | - {routes}/ //- Create folders for your routes |
| 68 | +| | | - index.ts //- APIs on `/` |
| 69 | +| | - models/ |
| 70 | +| | - utils/ |
| 71 | +| | - app.ts //- Module where Fastify is initialled |
| 72 | +| | - server.ts //- Entry point |
| 73 | +| - tests/ //- Unit tests |
| 74 | +| | - apis/ |
| 75 | +| | | - index.test.ts |
| 76 | +| | | - {routes}.test.ts |
| 77 | +| | - app.test.ts |
| 78 | +| | - tsconfig.json //- tsconfig to stop TypeScript & ESLint complaining |
| 79 | +| - .babelrc //- To correct import paths in `tsc` compiled files |
| 80 | +| - .eslintignore |
| 81 | +| - .eslintrc |
| 82 | +| - .gitignore |
| 83 | +| - jest.config.js |
| 84 | +| - LICENSE |
| 85 | +| - package-lock.json |
| 86 | +| - package.json |
| 87 | +| - README.md |
| 88 | +| - tsconfig.json |
| 89 | +``` |
| 90 | + |
| 91 | +## Author |
| 92 | +[Wing Chau](https://github.com/iamWing) [@Devtography] |
| 93 | + |
| 94 | +## Support the project |
| 95 | +Contributions via pull requests are welcome and encouraged. If there's anything |
| 96 | +you consider essential that should be included in this boilerplate, please don't |
| 97 | +hesitate to implement yourself and make a pull request :) |
| 98 | + |
| 99 | +Same as the other open sources projects in [@Devtography], I maintain & further |
| 100 | +develop this boilerplate with my free time. If you found my work useful and |
| 101 | +wanna support me keep on doing all these, please consider |
| 102 | +[donate/sponsor](https://github.com/sponsors/iamWing) me. |
| 103 | + |
| 104 | +## License |
| 105 | +TypeScript Fastify Backend Boilerplate is open source software |
| 106 | +[licensed as MIT](LICENSE). |
| 107 | + |
| 108 | +[@Devtography]: https://github.com/Devtography |
0 commit comments