Git Product home page Git Product logo

fastify-boilerplate's Introduction

Fastify Boilerplate

Initial setup

$ mkdir myapp && cd myapp
$ yarn init -y
$ yarn add fastify fastify-autoload fastify-cli fastify-plugin fastify-sensible
$ yarn add -D jest ts-jest @types/jest ts-node @types/node fastify-tsconfig typescript cross-env concurrently
$ mkdir -p src/routes test/routes
$ touch .gitignore tsconfig.json src/app.ts src/routes/root.ts test/helper.ts test/routes/root.test.ts

Config git

.gitignore

dist
node_modules
coverage

Initialize repository

$ git init

Config Typescript by extending fastify-tsconfig

tsconfig.json

{
  "extends": "fastify-tsconfig",
  "compilerOptions": {
    "outDir": "dist"
  },
  "include": ["src/**/*.ts"]
}

Config Typescript for Jest

$ yarn ts-jest config:init

Add "scripts" to package.json:

"scripts": {
  "test": "jest --coverage",
  "start": "npm run build:ts && fastify start -l info dist/app.js",
  "build:ts": "tsc",
  "dev": "tsc && concurrently -k -p \"[{name}]\" -n \"Typescript,App\" -c \"yellow.bold,cyan.bold\" \"tsc -w\" \"fastify start --ignore-watch=.ts$ -w -l info -P dist/app.js\""
},

Create main app

src/app.ts

import { join } from "path"
import AutoLoad from "fastify-autoload"
import { FastifyPluginAsync } from "fastify"

const app: FastifyPluginAsync = async (fastify) => {
  void fastify.register(AutoLoad, {
    dir: join(__dirname, 'routes')
  })
}

export default app
export { app }

Add routes

src/routes/root.ts

import { FastifyPluginAsync  } from "fastify"
const root: FastifyPluginAsync = async (fastify) => {
  fastify.get('/', async (req, res) => {
    return { root: true }
  })
}
export default root

Initialize app for tests

test/helper.ts

import Fastify from "fastify"
import fp from "fastify-plugin"
import App from "../src/app"

export function build() {
  const app = Fastify()

  beforeAll(async () => {
    void app.register(fp(App))
    await app.ready()
  })

  afterAll(() => app.close())

  return app
}

Test routes

test/routes.test.ts

import { build } from "../helper"

describe("Root route", () => {
  const app = build()

  it("works", async () => {
    const res = await app.inject({ url: "/" })
    expect(res.json()).toEqual({ root: true })
  })
})

First commit

$ git add .
$ git commit -m "Empty Fastify project"

Run tests

$ yarn test

Start on localhost

$ yarn dev

fastify-boilerplate's People

Contributors

julioprotzek avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.