Coder Social home page Coder Social logo

msanvarov / nest-rest-mongo-boilerplate Goto Github PK

View Code? Open in Web Editor NEW
272.0 5.0 39.0 18.14 MB

๐Ÿฑ backend with nest (typescript), mongoose, and authentication

Home Page: https://msanvarov.github.io/nest-rest-mongo-boilerplate/

License: MIT License

TypeScript 99.26% Dockerfile 0.74%
nest docker mongoose mongodb swagger typescript bearer-authentication nestjs fastify express

nest-rest-mongo-boilerplate's Introduction

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.

NPM Version Package License Travis

๐Ÿ“š Description

This boilerplate is made to quickly prototype backend applications. It comes with database, logging, security, and authentication features out of the box.


๐Ÿ› ๏ธ Prerequisites

Non Docker

  • Please make sure to either have MongoDB Community installed locally or a subscription to Mongo on the cloud by configuration a cluster in atlas.

Docker ๐Ÿณ

  • Please make sure to have docker desktop setup on any preferred operating system to quickly compose the required dependencies. Then follow the docker procedure outlined below.

Note: Docker Desktop comes free on both Mac and Windows, but it only works with Windows 10 Pro. A workaround is to get Docker Toolbox which will bypass the Windows 10 Pro prerequisite by executing in a VM.


๐Ÿš€ Deployment

Manual Deployment without Docker

  • Create a .env file using the cp .env.example .env command and replace the existing env variables with personal settings (MongoDB URL either srv or localhost)

    • Modify the connection string by modifying the following line.
  • Download dependencies using npm i or yarn

  • Start the app in pre-production mode using npm run start or npm run start:dev for development (the app will be exposed on the port 9000; not to conflict with React, Angular, or Vue)

Deploying with Docker ๐Ÿณ

  • Execute the following command in-app directory:
# creates and loads the docker container with required configuration
$ docker-compose up -d 

๐Ÿ”’ Environment Configuration

By default, the application comes with a config module that can read in every environment variable from the .env file.

APP_ENV - the application environment to execute as, either in development or production. Determines the type of logging options to utilize. Options: dev or prod.

APP_URL - the base URL for the application. Made mainly to showcase the power of ConfigService and can be removed as it doesn't serve any other purpose

WEBTOKEN_SECRET_KEY - the secret key to encrypt/decrypt web tokens with. Make sure to generate a random alphanumeric string for this.

WEBTOKEN_EXPIRATION_TIME - the time in seconds indicating when the web token will expire; by default, it's 2400 seconds which is 40 mins.

DB_URL - the URL to the MongoDB collection


๐Ÿ— Choosing a Web Framework

This boilerplate comes with Fastify out of the box as it offers performance benefits over Express. But this can be changed to use Express framework instead of Fastify.

For interchangeability:

  • Replace the following lines of code in the main.ts file with the ones detailed below.

Fastify:

// for fastify:
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import * as headers from 'fastify-helmet';
import * as fastifyRateLimiter from 'fastify-rate-limit';
const app = await NestFactory.create<NestFastifyApplication>(
  AppModule,
  new FastifyAdapter({ logger: console }),
);
app.register(headers);
app.register(fastifyRateLimiter, {
  max: 100,
  timeWindow: '1 minute',
});

Express:

// for express:
import * as headers from 'helmet';
import * as rateLimiter from 'express-rate-limit';
const app = await NestFactory.create(AppModule, {
  logger: console,
});
app.use(headers());
app.use(
  rateLimiter({
    windowMs: 60, // 1 minutes
    max: 100, // limit each IP to 100 requests per windowMs
  }),
);

Note: The boilerplate comes with production dependencies for both Express and Fastify to support moving between two. But this is going to leave it bloated especially when only one web framework is used at a time. Thus, it is recommended that when deploying to production, unused dependencies are purged.

If you choose to use Fastify, this command will purge all of the Express dependencies:

# removing Express dependencies
$ npm rm @nestjs/platform-express express-rate-limit helmet swagger-ui-express @types/express --save

If you choose to use Express, this command will purge all of the Fastify dependencies:

# removing Fastify dependencies
$ npm rm @nestjs/platform-fastify fastify-helmet fastify-rate-limit fastify-swagger --save

โœ… Testing

Docker ๐Ÿณ

# unit tests
$ docker exec -it nest yarn test

# e2e tests
$ docker exec -it nest yarn test:e2e

# test coverage
$ docker exec -it nest yarn test:cov

Non-Docker

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

๐Ÿ’ก TypeDocs

The documentation for this boilerplate can be found on Github pages.

The docs can be generated on-demand, simply, by typing npm run typedocs. This will produce a docs folder with the required front-end files and start hosting on localhost.

# generate docs for code
$ npm run typedocs

๐Ÿ“ Open API

Out of the box, the web app comes with Swagger; an open api specification, that is used to describe RESTful APIs. Nest provides a dedicated module to work with it.

The configuration for Swagger can be found at this location.


โœจ Mongoose

Mongoose provides a straight-forward, schema-based solution to model your application data. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box. Please view the documentation for further details.

The configuration for Mongoose can be found in the app module.


๐Ÿ”Š Logs

This boilerplate comes with an integrated Winston module for logging, the configurations for Winston can be found in the app module.


๐Ÿ‘ฅ Contribution

PRs are appreciated, I fully rely on the passion โค๏ธ of the OS developers.


License

Nest is MIT licensed.

Author

nest-rest-mongo-boilerplate's People

Contributors

msanvarov avatar renovate-bot avatar renovate[bot] 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  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  avatar

nest-rest-mongo-boilerplate's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

docker-compose
docker-compose.yml
dockerfile
Dockerfile
npm
package.json
  • @hapi/joi 17.1.1
  • @nestjs/common 8.4.7
  • @nestjs/core 8.4.7
  • @nestjs/jwt 9.0.0
  • @nestjs/mongoose 9.2.0
  • @nestjs/passport 8.2.2
  • @nestjs/platform-express 8.4.7
  • @nestjs/platform-fastify 8.4.7
  • @nestjs/swagger 5.2.1
  • @nestjs/typeorm 9.0.1
  • class-transformer 0.5.1
  • class-validator 0.13.2
  • crypto 1.0.1
  • dotenv 16.0.1
  • express-rate-limit 6.5.2
  • fastify-helmet 7.1.0
  • fastify-rate-limit 5.9.0
  • fastify-swagger 5.2.0
  • gravatar 1.8.2
  • helmet 5.1.1
  • mongoose 6.5.3
  • nest-access-control 2.2.0
  • passport 0.6.0
  • passport-jwt 4.0.0
  • reflect-metadata 0.1.13
  • rimraf 3.0.2
  • rxjs 7.5.6
  • swagger-ui-express 4.5.0
  • typeorm 0.3.7
  • winston 3.8.1
  • winston-daily-rotate-file 4.7.1
  • @compodoc/compodoc 1.1.19
  • @nestjs/testing 8.4.7
  • @types/dotenv 8.2.0
  • @types/express 4.17.13
  • @types/gravatar 1.8.3
  • @types/hapi__joi 17.1.8
  • @types/jest 28.1.8
  • @types/mongoose 5.11.96
  • @types/node 16.11.56
  • @types/supertest 2.0.12
  • chai 4.3.6
  • jest 29.0.0
  • nodemon 2.0.19
  • prettier 2.7.1
  • supertest 6.2.4
  • ts-jest 28.0.8
  • ts-loader 9.3.1
  • ts-node 10.9.1
  • tsc-watch 5.0.3
  • tsconfig-paths 4.1.0
  • tslint 6.1.3
  • typescript 4.7.4
travis
.travis.yml

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Implementation of roles using nest-access-control

First of all, nice library you have made here. I really appreciate your work.

I have a question regarding the implementation of roles using nest-access-control.

Your implementation currently seems to grant permission to "edit" and "delete" profile to all users (at least that is what happens when I try to run it).

I had much difficulty in finding good documentations of nest-access-control. So, my question is why is it that you attached ACGuard in the "delete" endpoint for profile and not in the "patch" endpoint? And is granting permission to all users intended in this particular boilerplate?

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.