Code Monkey home page Code Monkey logo

nestjs-boilerplate's Introduction

NestJS Boilerplate Nest Logo

Description

NestJS Boilerplate made with ❤️ by 💡VivifyIdeas💡.

Start Guide

Outside Docker containers

  • Create .env file cp .env.example .env and replace existing env variables (mysql/mariadb connection params)
  • Install dependencies yarn
  • Start the app yarn start (app will be exposed through the port 3000)

Inside Docker containers

Just run already prepared bash script:

$ ./init

It will setup the project for you (starting docker-compose stack, running migrations). The NestJS app running in dev mode will be exposed on http://localhost (port 80)

For IDE autocompletion to work, run yarn on the host machine.

TypeORM integrated

TypeORM gives you possibility to use next db types: mysql, postgres, mariadb, sqlite, etc. Please look at docs for more details. The docker-compose template uses mariadb.

Migrations

If you don't work on a production-ready project you can always change DB_SYNC env variable to true so you can play with NestJS without the need to write actual migrations.

synchronize: true shouldn't be used in production - otherwise, you can lose production data.

Create Migration

Creating new migration is relatively easy and you can use typeorm CLI for that. You can run this command to create new migration:

$ docker exec -it nest yarn migration:create -n {CreateTableUsers}

Migration file will be placed under src/migrations. For more details check the existing 1611484925515-CreateUsersTable.ts

Run Migrations

$ docker exec -it nest yarn migration:run

Revert Migrations

$ docker exec -it nest yarn migration:revert

Test

# 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

Environment Configuration

Integrated Configuration Module so you can just inject ConfigService and read all environment variables from .env file, which is created automatically by the init script from .env.example.

Swagger

RESTful APIs you can describe with already integrated Swagger. To see all available endpoints visit http://localhost/api/docs

Authentication - JWT

Already preconfigured JWT authentication. It's suggested to change current password hashing to something more secure. You can start use already working implementation of Login and Registration endpoints, just take a look at http://localhost/api/docs.

nestjs-boilerplate's People

Contributors

dependabot[bot] avatar fsschmitt avatar jovananov avatar miljanic avatar nikolasp avatar vivifyideas 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  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

nestjs-boilerplate's Issues

Register response

First of all, thanks for this valuable repository.

When I register I am getting back the same json form I sent in request like below.

{
  "expiresIn": "3600",
  "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNzE4MzI5OTczLCJleHAiOjE3MTgzMzM1NzN9.KUqmUSbb-2rRUm1sa4JZCKsJ-Fau7x5EBUVXu63PUIQ",
  "user": {
    "email": "[email protected]",
    "firstName": "First",
    "lastName": "Last",
    "password": "Cred)1",
    "passwordConfirmation": "Cred)1",
    "id": 1
  }
}

I can send a PR for it, if required?

Also, most or all of the commands for migrations have typo, they should simply be like below for local. Not sure for docker though.

$ yarn migration:create -n {CreateTableUsers}
$ yarn migration:run
$ yarn migration:revert

request /api/auth/me response "401 Unauthorized"

request /api/auth/me response "401 Unauthorized"
but i already set header “Authorization" token for login response

my curl
curl -X GET "http://localhost:3000/api/auth/me" -H "accept: application/json" -H "Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNTY4NTU5MzE4LCJleHAiOjE1Njg1NjI5MTh9.Irmkw2p2I_MiLcAxEqTpIR0GBhaDFbSYF3P4-ew5XEM"

but response "401 Unauthorized"

I hope to get a reply
Thank you

Help with building

Hey,

Great job making this boilerplate, I want to use it on a personal project. However, I am encountering some issues running start:prod ( 'module/config' was not found ) - maybe some issues with the tsconfig file.

Any suggestions on how to use this in a prod env?

error while installing project

./init

[2021-06-20T14:25:37+0300]: Looks like both docker and docker-compose are installed, everything looks good.

[2021-06-20T14:25:37+0300]: Copying .env.example -> .env

[2021-06-20T14:25:37+0300]: Starting docker-compose stack if not already started..

Traceback (most recent call last):
File "urllib3/connectionpool.py", line 670, in urlopen
File "urllib3/connectionpool.py", line 392, in _make_request
File "http/client.py", line 1255, in request
File "http/client.py", line 1301, in _send_request
File "http/client.py", line 1250, in endheaders
File "http/client.py", line 1010, in _send_output
File "http/client.py", line 950, in send
File "docker/transport/unixconn.py", line 43, in connect
FileNotFoundError: [Errno 2] No such file or directory

SyntaxError: Unexpected token const

When I run "npm run start:prod" the following error message appears:

`
(function (exports, require, module, __filename, __dirname) { declare const _default: {
^^^^^

SyntaxError: Unexpected token const
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:656:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Module.require (internal/modules/cjs/loader.js:636:17)
at require (internal/modules/cjs/helpers.js:20:18)`

Error on test

When I run npm test on default procject I get [ExceptionHandler] In order to use "defaultStrategy", please, ensure to import PassportModule in each place where AuthGuard() is being used. Otherwise, passport won't work correctly.

Switch to `nest-typed-config` for configuration

Nest-typed-config is a module which enables defining config schema like request DTOs, and it supports reading configuration from environment variables, local files or remote configuration center.

In this boilerplate, three types of configurations are included: App, JWT and DB, which can be defined in a local toml file for better organization:

# .env.toml
[App]
env = 'dev'
url = 'http://localhost'

[JWT]
secretKey = 'uAsBw6WxqD'
expirationTime = 3600

[DB]
type = 'mariadb'
username = 'nest'
password = 'nest'
host = 'db'
port = 3306
database = 'nest'
sync = false

More importantly, nest-typed-config provides type-safe configurations, for example in app.module.ts:

TypeOrmModule.forRootAsync({
    // DBConfig is defined in config schema, and automatically injected
    useFactory: (dbConfig: DBConfig) => {
        return {
            ...dbConfig,
            entities: [__dirname + './../**/**.entity{.ts,.js}'],
            synchronize: dbConfig.sync,
        }; // No type casting is required
    },
})

Please let me know your thoughts, and I can open a pull request if this sounds good.

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.