node-service-template
provides a "battery-included" starter template for building enterprise Node.js webservices.
It comes with the following out-of-the-box:
- fastify-based general application skeleton;
- Modular, domain-driven structure that encourages separation of concerns;
- Server/app separation, for convenient bootstrapping in e2e tests;
- Global error handler;
- JSON-based, single line standardized logging;
- Populates
req.id
for incoming requests based onx-request-id
header, or generates new UUID if none is set.
Mechanisms:
- Type-safe config management;
- Dependency injection (using awilix);
- Scheduling (using toad-scheduler and redis-semaphore);
- Type-safe dependency mocking for tests;
Scaffolding:
Basic building block examples:
- Repository (using prisma);
- Domain service;
- Controller;
- Route;
- Schema;
- e2e test;
Plugins:
- Smart healthcheck plugin (using fastify-custom-healthcheck);
- JWT plugin (using @fastify/jwt);
Scripts:
Service template also comes with a curated set of plugins installed:
- @fastify/helmet (security headers)
- @fastify/swagger (OpenAPI specification generation)
- @fastify/swagger-ui (visualizing OpenAPI specification)
- @fastify/awilix (dependency injection)
- @fastify/schedule (scheduling background jobs)
- @fastify/auth (authentication)
- fastify-graceful-shutdown (handling SEGTERM gracefully)
- fastify-no-icon (avoiding warnings when sending GET calls via browser)
- fastify-custom-healthcheck (registering app and dependency healthchecks)
- @lokalise/fastify-extras -> metricsPlugin (exposing Prometheus metrics)
- @lokalise/fastify-extras -> requestContextProviderPlugin (storing requestId in AsyncLocalStorage and populating requestContext on request)
- @lokalise/fastify-extras -> newRelicTransactionManagerPlugin (creating custom NewRelic spans for background jobs)
- @lokalise/fastify-extras -> bugsnagPlugin (reporting errors to BugSnag)
- @lokalise/fastify-extras -> prismaOtelTracingPlugin (generating OpenTelemetry metrics for DB operations using prisma)
Note that some of the fastify-extras plugins may not be relevant for you (e. g. if you are not using Prometheus, New Relic or Bugsnag). In that case you should remove the plugins and delete everything that breaks when you attempt to build the project.
We recommend you to create your own @your-org/fastify-extras
package and create your own mix of vendor plugins that
are relevant for the technological stack of your organization, and replace @lokalise/fastify-extras
with it.
- Install all project dependencies:
npm install
- Copy the
.env.default
file to a new.env
file. You can do this with the following npm script:
npm run copy:config
- Run migrations to synchronize your database schema with defined models.
npm run db:migration:dev
- Generate Prisma client for type-safe DB operations:
npm run db:update-client
- You can use Docker Compose for launching all the infrastructural dependencies locally:
docker compose up -d
- To register local dev requests in newrelic please use:
NEW_RELIC_APP_NAME=dev.yourapp.yourdomain.com NEW_RELIC_LICENSE_KEY=<license_key> npm run start:dev