• Stars
    star
    329
  • Rank 128,030 (Top 3 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created almost 2 years ago
  • Updated 17 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Batteries-included starter template for Node.js backend services

node-service-template

Overview

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 as a basis for the general web 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;
  • Automatic population of req.id for incoming requests based on x-request-id header, or generation of new UUID if none is set, for the purposes of distributed tracing.

Mechanisms:

Scaffolding:

Basic building block examples:

Plugins:

Scripts:

  • Generate OpenAPI specification from your route definitions;
  • Validate your OpenAPI specification.
  • GenerateJwt generate jwt for dev usage.

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.

Getting Started

  1. Install all project dependencies:
npm install
  1. Copy the .env.default file to a new .env file. You can do this with the following npm script:
npm run copy:config
  1. Run migrations to synchronize your database schema with defined models.
npm run db:migration:dev
  1. Generate Prisma client for type-safe DB operations:
npm run db:update-client
  1. You can use Docker Compose for launching all the infrastructural dependencies locally:
docker compose up -d
  1. To run application:
npm run start:dev

OpenAPI specification

You can access OpenAPI specification of your application, while it is running, by opening SwaggerUI

Create jwt for dev usage

By default, all calls to the node-template app will require a valid JWT token.

You have multiple options to ease your development:

  1. Comment onRequest hook for JWT verification here
  2. Generate a valid JWT with provided generateJwt script

Generate JWT script

  • Script requires public-private key pair encrypted with RS256 algorithm. Run it in keys directory.
ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
# Don't add passphrase
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub

or if you are in node-service-template dir

ssh-keygen -t rsa -b 4096 -m PEM -f ./scripts/keys/jwtRS256.key
# Don't add passphrase
openssl rsa -in ./scripts/keys/jwtRS256.key -pubout -outform PEM -out ./scripts/keys/jwtRS256.key.pub
  • Default file names are jwtRS256.key for private key and jwtRS256.key.pub for public key
  • Run JWT generate script
npm run jwt:generate
  • Copy your public key to .env file as a one line string, it will be printed with a token in console to make things easier. Example output:
Public key:
-----BEGIN PUBLIC KEY-----||MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAt+2fSaw+mjbQpbPYcGv7||A0zs+P1yuCcM4LzTRpMmtXCoxCg3hwVZUM9HoxM4NxSga5A/jdHDhn1qEgQF38cX||N/wG+cRx1YfxDV2fSYxO9ouh+0J+uJaAXs0kWM0oAojrcMI4q1PcTeCFBvKDR+ei||Nu5auiRe7yrBfQTqsSmvEDRlnhUnF24CnNQPuzeN4Qe8LmcXuwimEyAi9Tf7hXBN||H31j+jnUfIq9Yy7EsbmZhW3aEmQlmR6RY/9g+IEzbpmBoYznYsxmvtODpay7n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9ohGoNzk-axmB5QG1PvLYpZ2_t0z7h5od2vw5ZTPNOQ-RhNSu28REd4Mp0xHySYsn0ukf4kZHPUoGbMIuIMg6WhVTsz7V4n0nd1iPIjBfJjWM5dDSZfQvg4whwO1jeaE4BXxpjeiFqxf_tOT1QM
Verified payload:
{"claim":"value","iat":1676958080}
  • Use token from the console in your requests

More Repositories

1

i18n-ally

🌍 All in one i18n extension for VS Code
TypeScript
3,971
star
2

lokalise-cli-2-go

Lokalise CLI v2
Go
107
star
3

node-lokalise-api

Lokalise API v2 Node.js client.
TypeScript
61
star
4

lokalise-ios-framework

Lokalise iOS SDK
Objective-C
50
star
5

lokalise-tutorials

Source code for technical tutorials hosted on Lokalise.
PHP
36
star
6

lokalise-fastlane-actions

Ruby
29
star
7

ruby-lokalise-api

Lokalise API v2 Ruby interface.
Ruby
19
star
8

go-lokalise-api

Lokalise API v2 Golang client library.
Go
17
star
9

php-lokalise-api

Lokalise API v2 PHP library.
PHP
16
star
10

python-lokalise-api

Lokalise API v2 official Python library
Python
13
star
11

lokalise-android-sdk

10
star
12

elixir-lokalise-api

Official Lokalise APIv2 interface for Elixir.
Elixir
9
star
13

lokalise-cli-go

Lokalise CLI v1 source code. Use CLI v2 instead.
Go
6
star
14

i18n-ally-jetbrains-docs

HTML
6
star
15

npm-package-template

Template repository for creating NPM packages
JavaScript
5
star
16

fastify-extras

Reusable plugins for fastify
TypeScript
4
star
17

node-core

Core libraries for Node.js backend services
TypeScript
4
star
18

homebrew-cli-2

Lokalise CLI v2 Homebrew repo.
Ruby
3
star
19

apps

Lokalise demo apps
3
star
20

shared-ts-libs

Shared TypeScript libraries
TypeScript
2
star
21

ai-emporium-lokalise-demo-app

Demo application showcasing the integration and automation capabilities of GitHub Actions with Lokalise.
JavaScript
2
star
22

zod-extras

Adds additional functionality on top of zod framework
TypeScript
2
star
23

styled

React styling solution for Lokalise org
TypeScript
2
star
24

lokalise-cli

Lokalise CLI v1 Dockerhub repo.
Dockerfile
2
star
25

docker-webpack

A simple docker image with webpack
Dockerfile
1
star
26

node-utils

General collection of useful reusable Node.js code
1
star
27

ce-connector-api

Connector API for Content type app engine
1
star
28

meekrodb

PHP
1
star
29

live-edit-ios

Lokalise iOS SDK Live Edit Module
Objective-C
1
star
30

ce-connector-template-node

NodeJs connector template for Content type app engine
TypeScript
1
star
31

eslint-plugin

lokalise eslint plugin
JavaScript
1
star
32

frontend-http-client

Opinionated HTTP client for the frontend
TypeScript
1
star