Skip to content

peterdee/fastify-generic

Repository files navigation

fastify-generic

Generic Fastify project with API documentation, API testing, authorization, database connection, Redis caching, etc.

Some of the code is typed with JSDoc, but types are not enforced (not using @ts-check directive)

Deploy locally

git clone https://github.com/peterdee/fastify-generic
cd ./fastify-generic
nvm use 20
npm ci

Environment variables

The .env file is required, see .env.example for details

Launching the server

Without Docker container

Launch in development with hot reloading:

npm run dev

Launch in production:

npm start

Server will be available at http://localhost:9999 (port 9999 is a default one, you can change it in .env file)

With Docker container

Make sure that application uses port 9999!

Build an image:

docker build -t fastify-generic .

Run container:

docker run -p 9999:9999 -d fastify-generic

Server will be available at http://localhost:9999

API documentation

API documentation is generated using apiDoc

In order to generate documentation, apiDoc should be installed globally:

npm i -g apidoc

Generate documentation:

npm run docs

Documentation will be available at http://localhost:9999/docs/ once you start the server, however it will be served only if APP_ENV environment variable is set to development

Testing

Tests are done with node:assert and node:test

Tests are using mongodb-memory-server to emulate MongoDB connection, but there are no emulation for Redis (currently no modules are available for that): when tests are running, the app will try to connect to the Redis server specified by the REDIS_TEST_CONNECTION_STRING environment variable

Run all tests:

npm run test

It is possible to run a specific test (for example just a single controller test):

export APP_ENV=testing && node --test ./apis/me/controller.test.js

Linting

Using ESLint

License

MIT