nestjs-rest-scaffold

REST boilerplate to build APIs / microservices using the NestJS framework

MIT License

Stars
3
Committers
3

About The Project

NestJS is a great Framework to start building very robust server-side applications. There are many great templates available on GitHub; however, I didn't find one that really suited my needs so I created this very opinionated one. I wanted a boilerplate that would integrate many of the great features Nest has to offer, out-of-the-box, with the minimun configuration effort to start bootstraping an application right away.

Here's why:

  • Your time should be focused on creating something amazing. A project that solves a problem and helps others
  • You shouldn't be doing the same tasks over and over; like integrating a DB layer, a microservice layer, etc.
  • You should implement DRY principles to the rest of your life as well :)

Of course, no one template will serve all projects since your needs may be different, but here's for everyone who find it helpful.

Built With

Features

Every time I start building an API, a REST microservice, a server-side app; there's always a set of features that I end up reusing over and over given the utility they provide. That's the main reason I have decided to create this project to bootstrap as much of this core functionality as possible.

Let's list a few of the features provided on this project:

  • Open Telemetry to instrument, generate, collect, and export telemetry data.
  • Terminus module for the healthcheck of your application exposed at /health route.
  • OpenAPI specification (Swagger) for all the HTTP routes (Thanks to NestJS CLI plugin).
  • Global exception middleware so you can catch and customize any target exception to your likings.
  • Global request-logger middleware so all the incoming and outgoing HTTP requests are logged by default with their metadata.
  • Global Cache middleware to enhance the response time of all the GET HTTP requests.
  • A programmatic way to access environment variables through DI.
  • A database ORM to interact with mulple DB drivers using the great Prisma package.
  • A programmatic way to generate logs using DI upon the right context of your application.
  • An example implementation of a Kafka transporter layer with a producer/consumer interface.
  • Husky package with lint-staged bundled with pre-commit and pre-push hooks that will run linter, formatter and tests

Folder Structure

Following the Nest way of things, I ended up up with a module-based approach when it came to structuring the code. That being said, the project comes with a series of folders and modules that you can reuse and extend to fit your needs, let's talk a bit about it.

  • config Like its name implies is a folder to place all sort of configurations for your app. cache, env, configurations live here. You can also and include more modules in it.
  • config/otel-tracer Open Telemetry SDK and configuration file.
  • config/db Quite self explanatory, this is your DB module, the folder to place all your configs and ORM layer.
  • config/logs Module where you can place the log driver used by your app. Comes with Pino and Winston out of the box.
  • core This is it, the source of evil the base code. It's a module wrapping the minimun functionality the app should have, again this is very opinionated of me, so you may choose to adjust it to fit your needs.
  • providers Folder to place external third-party SDKs or libraries (AWS, Sendgrid, etc).
  • resources Interfaces and endpoints with the bussiness logic of your app should be placed here. Modules, Controllers, Services and entities related to the solution you're building.
  • resources/healthcheck This exposes the healthcheck functionality of your app to the HTTP transport layer.
  • transporters Meant for all the microservices and interfaces other than HTTP used in your app. Kafka, Redis, NATS, etc.

Getting Started

To get a local copy up and running follow these simple example steps.

Prerequisites

  • npm
    npm install npm@latest -g
    
  • Nest
    npm i -g @nestjs/cli
    

Installation and Usage

  1. Clone the repo
    git clone https://github.com/your_username_/Project-Name.git
    
  2. Install NPM packages
    npm install
    
  3. Run the application
    npm run start
    

Roadmap

  • Add unit tests
  • Add e2e tests

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE.md for more information.

Related Projects