YSM Backend
A NestJS API server with Jest testing.
If you'd like to help Chayn by tackling any of our open Github issues, please get in touch with us to express your interest in volunteering via this form. We'll get back to you to schedule an onboarding call.
Development
Note: if you just want to run the backend service locally and not do any development work on it, you can instead skip to the section on how to run it in a Docker container.
Prerequisites
- NodeJS v12+
- Yarn v1.21+
Set up local env config
Certain config values are required to run the server.
For local development, create a new .env.development
file and add in the following:
STORYBLOK_TOKEN={value} # The API token from Storyblok (must have 'draft' access)
FIREBASE_SERVICE_ACCOUNT={value} # The service account JSON object serialised into a string and then base64 encoded
CONTENT_EDITOR_EMAILS={value} # Optional. A comma separated list of email addresses of the users that are allowed to access preview mode (for viewing draft content from Storyblok)
ROLLBAR_TOKEN={value} # Optional when running in `dev` mode. Either set this to the Rollbar server token, or to `false` to disable.
ROLLBAR_ENV=local-dev # Required in `production` mode or if `ROLLBAR_TOKEN` is set.
RATE_LIMIT_WINDOW_MS={value} # Optional. The window of time (in milliseconds) for the rate limiting to apply.
RATE_LIMIT_MAX=(value) # Optional. The max number of requests (per IP address) within the window of time (above).
Env config for tests
Tests will use a separate .env.test
file which should already be present.
You'll also need to create a .env.test.local
file (see below for more details).
All access to external services in unit tests should be mocked out, so when adding new config to the app make sure to add a dummy 'noop' value in the .env.test
file and commit to the repo.
It's also advisable to mock out access to external services in the e2e tests (e.g. like with Storyblok), but sometimes it makes sense to actually call the external service (like for Firebase Auth tokens). In the latter case, make sure all "live" config is set in a .env.test.local
file (which must NOT be committed to the repo), with the following:
FIREBASE_SERVICE_ACCOUNT={value} # Same as in .env.development - the service account JSON object serialised into a string and then base64 encoded
FIREBASE_WEB_API_KEY={value} # Special API key just for use in e2e tests - found in the settings page for the Firebase project
Install dependencies
yarn
Run locally
# development
$ yarn start
# watch mode
$ yarn start:dev
# production mode
$ yarn start:prod
Run tests
# unit tests
$ yarn test
# e2e tests
$ yarn test:e2e
# test coverage
$ yarn test:cov
Formatting and linting
yarn lint
To lint and fix:
yarn lint:fix
Formatting and linting is provided by ESLint and Prettier (see the relevant configs for details).
Workspace settings for VSCode are included in this folder.
Build the app for production
yarn build
Generating new modules, controllers, services, etc
NestJS provides the nest generate
command to help generate relevant files.
axios
requests (e.g. for Storyblok requests)
Debug logs for all To see debug logs for all axios requests made – e.g. by the Storyblok client – start the dev server with:
DEBUG=axios yarn start:dev
Note: the axios-debug-log
library used to provide this logging has only been added as a dev dependency, so this will not work in production environments.
Running as a Docker container locally
You may want to run the backend service in a Docker container if:
- You don't intend to do any development work on it and just need a running service for the frontend to access.
- You want to test that the Docker image works as expected, e.g. if you've made any changes to the
Dockerfile
.
First, ensure you have the Docker service installed and running on your machine. More info on how to do this: https://docs.docker.com/get-docker/.
Then, follow the section on setting up your local env config, above. Note that you don't need to follow any other instructions from the previous sections (like having the prerequisites, installing dependencies, etc.) as the Docker build process will do all this for you.
Then, build the image:
docker build -t ysm-backend .
Then, run the backend service in a container:
docker run --rm -it -p 3000:3000 --env-file=.env.development -e PORT=3000 --init ysm-backend
You can now test that the service is running, either using curl
:
curl -v http://localhost:3000/api/resources
… or opening the URL http://localhost:3000/api/resources in your browser. It should show the JSON output of the /resources
API.