GithubHelp home page GithubHelp logo

neosoft-technologies / rest-node-nestjs Goto Github PK

View Code? Open in Web Editor NEW
53.0 4.0 36.0 8.85 MB

With this skeleton, we can easily start a server application using Node.js, Nest JS and TypeScript.

Home Page: https://neosoft-technologies.github.io/rest-node-nestjs/

License: Apache License 2.0

Shell 0.34% JavaScript 2.62% TypeScript 95.69% Dockerfile 0.25% Pug 1.10%
restful-api best-practices nestjs typescript docker node typeorm nodejs jest boilerplate

rest-node-nestjs's Introduction

Nest JS Skeleton for REST Application Quality Gate Status CI

A skeleton/boilerplate/starter project for quickly building RESTful APIs using Node.js, NestsJS, Express, and MySQL.

By running one command, you will get a production-ready Node.js app installed and configured on your machine. There are many built-in features in the skeleton, including authentication using JWT, request validation, unit and integration tests, continuous integration, docker support, API documentation, pagination, etc. To learn more about its features, check out the following list.

Description

  • Nest framework TypeScript starter repository.

  • Nest provides a level of abstraction above these common Node.js frameworks (Express/Fastify) but also exposes their APIs directly to the developer. This allows developers the freedom to use the myriad of third-party modules which are available for the underlying platform.

  • There are superb libraries, helpers, and tools that exist for Node (and server-side JavaScript), none of them effectively solve the main problem of โ€” Architecture.

Take it for a test drive. We'd love to hear any feedback you have or if you've thought of a new feature.

Table of Contents

Features

  • Quick start
  • Integrated ESLint, Prettier and Husky
  • Simple and Standard scaffolding
  • Production-Ready Skeleton
  • Followed SOLID Principles
  • Authentication and authorization: using passport
  • Validation: request data validation using Nest JS Pipe
  • Logging: using winston
  • Testing: unit and integration tests using Jest
  • Error handling: centralized error handling mechanism
  • API documentation: with swagger
  • Process management: advanced production process management using PM2
  • Dependency management: with npm
  • Environment variables: using dotenv and cross-env
  • API Versioning
  • Security: set security HTTP headers using helmet
  • Santizing: sanitize request data against xss and query injection
  • CORS: Cross-Origin Resource-Sharing enabled using cors
  • Compression: gzip compression with compression
  • CI: Continuous integration with Travis CI
  • Docker support
  • Git hooks: with husky and lint-staged
  • Linting: with ESLint and Prettier
  • Editor config: consistent editor configuration using EditorConfig

Getting started

Prerequisites

Setup

To get started, clone the repository to your local computer. Use the following command to run in your terminal.

Clone The Application

// clone the application
$ git clone https://github.com/NeoSOFT-Technologies/rest-node-nestjs.git

Install The Dependencies

Next, install the packages that are required for this project.

// Install the required npm modules
$ npm install

Create The Environment Variables

The config/env/.env file should be placed in root folder with the following variables.

  • config/env/.env : Default Environment File
  • config/env/.env.dev : Development Environment File
  • config/env/.env.test : Test Environment File
  • config/env/.env.prod : Production Environment File
# config/env/.env.example

APP_NAME=rest_api
NODE_ENV=dev
DB_HOST=127.0.0.1
DB_DATABASE=rest_api
DB_USER=user
DB_PASSWORD=root
DB_PORT=3306

Start MySQL Database

In order to use mysql, you need to have it installed in your local machine. Docker Compose is what we will be using in our case, In the project directory, execute the following command.

# build images, create and run containers in background
docker-compose -f ./docker/docker-compose.yml --env-file ./config/env/.env up -d

In order to apply your modified code to a running container, you should add a build option.

# if source code is changed, rebuild image, recreate and start container
docker-compose  -f ./docker/docker-compose.yml --env-file ./config/env/.env up -d --build

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

For this project, We chose Jest as our test framework. While Mocha is probably more common, Mocha seems to be looking for a new maintainer and setting up TypeScript testing in Jest is wicked simple.

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Running the build

All the different build steps are orchestrated via npm scripts. Npm scripts basically allow us to call (and chain) terminal commands via npm. This is nice because most JavaScript tools have easy to use command line utilities allowing us to not need grunt or gulp to manage our builds. If you open package.json, you will see a scripts section with all the different scripts you can call. To call a script, simply run npm run <script-name> from the command line. You'll notice that npm scripts can call each other which makes it easy to compose complex builds out of simple individual build scripts. Below is a list of all the scripts this template has available:

Npm Script Description
build Full build. Runs ALL build tasks
start:debug Performs a full build and then serves the app in watch mode
lint Runs ESLint on project files
format Runs the file formatter
serve Runs node on dist/server.js which is the apps entry point
start Does the same as 'npm run serve'. Can be invoked with npm start
test Runs tests using Jest test runner
test:watch Runs tests in watch mode
doc Generate the project documenation using compdoc

Project Structure

In a TypeScript project, it's best to have separate source and distributable files. TypeScript (.ts) files live in your src folder and after compilation are output as JavaScript (.js) in the dist folder. The test and views folders remain top level as expected.

Please find below a detailed description of the app's folder structures:

Note! Make sure you have already built the app using npm run build

Name Description
.vscode Contains VS Code specific settings
.github Contains GitHub settings and configurations, including the GitHub Actions workflows
dist Contains the distributable (or output) from your TypeScript build. This is the code you ship
node_modules Contains all your npm dependencies
src Contains your source code that will be compiled to the dist dir
src/config Here you will find all the environment configuration necessary to access the application e.g. .env
src/components Components define group of files/source that respond to various module(http requests)
src/components/${module_name}/dto/ DTO (Data Transfer Object) Schema, Validation
src/components/${module_name}/entities/ Entities belongs to that Component
src/components/${module_name}/repository/ Repository belongs to that Component
src/components/${module_name}/services/ Services belongs to that Component
src/components/${module_name}/module_name.controllers.ts Controller belongs to that Component
src/components/${module_name}/module_name.module.ts Module belongs to that Component
src/core All core modules - Guards, Http Request & Response Handler, Logger
src/main.ts Entry point to your express app
test Contains your tests. Separate from source because there is a different build process.
config/env/.env.example API keys, tokens, passwords, database URI. Clone this, but don't check it in to public repos.
package.json File that contains npm dependencies
tsconfig.json Config settings for compiling server code written in TypeScript
tsconfig.build.json Config settings for compiling tests written in TypeScript
.eslintrc Config settings for ESLint code style checking
.eslintignore Config settings for paths to exclude from linting

Documentation

1. Generation with compdoc

Generate project documentation using the following command (npm 6 is required for npx support). See the official documentation for more options.

npm run doc

OR

npx @compodoc/compodoc -p tsconfig.json -s

An explanation of how the request and response cycle works is provided here

2.1 Request and Response Workflow

Boilerplate has a custom guard enabled for handling response and request for every api. The integration of request response guard is enabled by default with response structure

Request and Response Cycle

2.2 Request Workflow

By creating a workflow, you can specify the template that should be used to create a change request when a request for service is logged.

Request Workflow

3. Modules

3. Modules

Miscellaneous

Trainings

Video Tutorials

Below are the video tutorial links for the modules that we have implemented in the boilerplate.

The Below list is the demonstration Videos of the various modules in the nestjs boilerplate.

The below list will tell us how to create components in the boilerplate.

Testing Videos

Contributing To This Project

Contributions are welcome from anyone and everyone. We encourage you to review the guiding principles for contributing

Issues and Discussions

Stay in touch

rest-node-nestjs's People

Contributors

charchitd avatar dependabot[bot] avatar nst-coe-sasachan avatar pareshjoshi2468 avatar prajwalkarale08 avatar rkg1007 avatar santoshneo avatar santoshshinde2012 avatar sasachan avatar ssingh3006 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rest-node-nestjs's Issues

EP - Basic CRUD API

This API performs the basic CRUD functionalities by directly communicating with the database in the back-end of the application

EP - WebSocket Integration

Basic Socket.io - Websocket integration

Originally posted by @PareshNeosoft in #114
A WebSocket is a persistent connection between a client and server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

T- Crypto Integration with Request Handler

Integrating the encryption-decryption module with request handler. This will encrypt the request coming from the user and then we have to decrypt it in the back-end.

EP - Folder Structure & Stable Dependency

For every module, there could be many npm to be used/ module that can be used as per usage/ different integrations.

for the folder structure, just an idea about creating folder structure under src as the 'src/purpose of module/integrationName'

for example: payment:
'src/payment/razorPay'
for example email:
'src/email/sendgrid'

Originally posted by @suresh-shetiar in #30

  • T - Folder Structure
  • T - Folder Documentations and Naming Conventions

EP - Unit & Integration Test Cases

Always write unit test case for every api

Originally posted by @arya0618 in #18

To check whether the given api is properly calling the required methods or not or proper service of the api is being executed or not.

EP - Cache Helper

Identifying the correct storage options for assets and media files like s3 or cloud storage is very important aspect of any project architecture. In addition to this deciding on caching strategies also plays key role in project performance in long run

Originally posted by @adnankhan-neo in #52

EP - NoSQL database: MongoDB

NoSQL database: MongoDB object data modeling using Mongoose

  • Create Basic Connection with Mongoose
  • Defining Mongoose Model
  • CRUD Operations

EP - Multi Environment Support

Discussed in #38

Originally posted by parthdave0017 August 10, 2021
We should be able to configure multiple envs, as most major projects will atleast need dev, staging & production as a different setup.

EP - Folder Structure

For every module, there could be many npm to be used/ module that can be used as per usage/ different integrations.

for the folder structure, just an idea about creating folder structure under src as the 'src/purpose of module/integrationName'

for example: payment:
'src/payment/razorPay'
for example email:
'src/email/sendgrid'

EP - ACL and Role Management

Discussed in #51

Originally posted by suresh-shetiar August 17, 2021
ACL- Access control list and role management plays a very important role in any project and adds a extra benefit in security terms about who is accessing the resources.

EP - Encryption Decryption Model

We should have an encryption and decryption model, such that we can encrypt and decrypt important data.

Originally posted by @mdmudassiriqbal in #42

Various inbuilt hashing algorithms are available such as AES(Advanced Encryption System)can be used for encryption which is available through the crypto module in NodeJS. This will be important for saving the user details in an encrypted format.

EP - Compression

Compression: gzip compression with compression

Originally posted by @shishirYadav in #13

Compression can greatly decrease the size of the response body, thereby increasing the speed of a web app.
Using compression and decompression we can send HTTPS requests and responses easily.

EP - Payment Integration

Idea is to have sample code for various type of payment integration and redme about each payment integration. Like if we have sample code for razorPay integration then we should also have readme for it about how to use it and in which cases it could be useful. Along with it additional information about in which countries this payment integration would work for.

the folder structure to be like src/paymentIntegration/razorPay

Originally posted by @suresh-shetiar in #28

EP - Versioning Support for the APIs

Discussed in #40

Originally posted by parthdave0017 August 10, 2021
We need to be able to support multiple versions for each API endpoint, this can be helpful specifically when APIs are consumed by mobile & desktop applications.

EP - AWS Support & S3

To maintain sensitive data like db/app configuration we can use parameter store.
Instead of .env file we can store all sensitive information/configurations on AWS parameter store and fetch as per requirement, this will avoid unwanted data modification on the developer end.

Originally posted by @Riyazm16 in #60

EP - CI & CD Support

CI/CD is a method to frequently deliver apps to customers by introducing automation into the stages of app development.
Using CI & CD for deploying the build to SIT, UAT and Prod environments.

EP - Email Handler

We have multiple platforms available which allows us to send an email. The idea is to have a sample code for email sender with multiple types of integration.
Email sender with NodeMailer, Sendgrid, AWS SES, and others which are famous and available as per the need

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.