|
|
||
|---|---|---|
| docs | ||
| src | ||
| tests | ||
| .env.example | ||
| .gitignore | ||
| .prettierignore | ||
| .prettierrc.json | ||
| docker-compose.yml | ||
| Dockerfile | ||
| package-lock.json | ||
| package.json | ||
| README.md | ||
| tsconfig.json | ||
Fastify Node.js Projekt Setup
Dieses Repository ist ein vorbereitetes Template für ein modernes Node.js + TypeScript + Fastify Projekt mit Prettier, Vitest, TSX und Swagger.
Voraussetzungen
- Node.js >= 20.6.0
- npm
Projekt initialisieren
Es wird empfohlen, das Projektverzeichnis bereits vorab mit Git zu initialisieren:
git init
Anschließend kann das Projekt mit npm initialisiert werden:
npm init
Während der npm-Initialisierung kannst du alle Fragen einfach mit Enter bestätigen und die Default-Werte übernehmen. Es ist nicht notwendig, Felder wie Name, Description, Author oder Main individuell auszufüllen.
Wichtige Hinweise:
- Alle Werte wie
description,authorodermainkönnen später problemlos wieder aus derpackage.jsonentfernt oder angepasst werden. - Das Feld
mainwird in diesem Projekt nicht benötigt und kann nachträglich gelöscht werden. - Ziel ist lediglich, eine gültige
package.jsonzu erzeugen, die anschließend manuell angepasst wird.
.gitignore anlegen
Datei .gitignore erstellen mit folgendem Inhalt:
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.*
!.env.example
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
.output
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Sveltekit cache directory
.svelte-kit/
# vitepress build output
**/.vitepress/dist
# vitepress cache directory
**/.vitepress/cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# Firebase cache directory
.firebase/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v3
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
# Vite files
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
.vite/
# General
.DS_Store
__MACOSX/
.AppleDouble
.LSOverride
Icon[]
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
.vscode
tmp*
TypeScript installieren
npm install --save-dev typescript
npm install --save-dev @types/node
tsconfig.json
Lege eine tsconfig.json mit folgendem Inhalt an:
{
"compilerOptions": {
"rootDir": ".",
"outDir": "./dist",
"module": "nodenext",
"target": "ES2022",
"sourceMap": true,
"declaration": true,
"declarationMap": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"strict": true,
"verbatimModuleSyntax": true,
"isolatedModules": true,
"noUncheckedSideEffectImports": true,
"moduleDetection": "force",
"skipLibCheck": true
},
"include": ["src", "tests"]
}
TSX installieren (Dev Runner)
npm install --save-dev tsx
Prettier Setup
Installation
npm install --save-dev --save-exact prettier
.prettierrc.json
{
"semi": false,
"trailingComma": "es5",
"printWidth": 80,
"tabWidth": 4,
"useTabs": true,
"arrowParens": "always",
"endOfLine": "lf"
}
.prettierignore
Datei .prettierignore anlegen:
node_modules
dist
build
.next
coverage
tmp
*.log
.git
Environment Files
Lege folgende Dateien an:
.env.env.example
Beispiel .env.example:
PORT=3000
Test Framework (Vitest)
npm install -D vitest
Projektstruktur:
src/
index.ts
tests/
example.test.ts
package.json anpassen
Scripts
"scripts": {
"dev": "tsx watch --env-file=.env src/index.ts",
"build": "tsc",
"start": "node --env-file=.env dist/index.js",
"test": "vitest",
"test:run": "vitest run",
"prettier": "prettier --write ."
}
Engines
"engines": {
"node": ">=20.6.0"
}
Module Type
"type": "module"
Fastify Setup
Installation
npm install fastify
npm install @fastify/swagger
npm install @fastify/swagger-ui
Swagger
Die Swagger UI soll unter folgender URL erreichbar sein:
http://localhost:3000/swagger
Konfiguriere @fastify/swagger-ui entsprechend mit:
routePrefix: '/swagger'
Wichtige Hinweise
- In der
package.jsonmuss keinmainFeld gesetzt werden. - Das Projekt nutzt ES Modules:
"type": "module"
- Node Version über Engines absichern:
"engines": {
"node": ">=20.6.0"
}
Scripts (package.json)
"scripts": {
"dev": "tsx watch --env-file=.env src/index.ts",
"build": "tsc",
"start": "node --env-file=.env dist/src/index.js",
"test": "vitest",
"test:run": "vitest run",
"prettier": "prettier --write ."
}
Dieses Repository dient als sauberes Basis-Template für ein modernes Fastify + TypeScript Backend ohne Beispiel-Endpunkte oder Demo-Code.