Skip to content

Instantly share code, notes, and snippets.

@r37r0m0d3l
Forked from aaabramov/01_core.md
Created January 17, 2022 16:35
Show Gist options
  • Save r37r0m0d3l/d1d1b9f7f7baf1d0df2efccbb1bbd76d to your computer and use it in GitHub Desktop.
Save r37r0m0d3l/d1d1b9f7f7baf1d0df2efccbb1bbd76d to your computer and use it in GitHub Desktop.
Typical NestJS app
# Generate new service
nest new <service_name>
# All services need this
npm install \
class-validator \
class-transformer \
@nestjs/typeorm \
typeorm \
pg \
@nestjs/terminus \
@nestjs/config \
@nestjs/swagger \
swagger-ui-express \
dotenv
# Types
npm install --save-dev \
@types/validator
# You have auth?
npm install \
@nestjs/passport passport \ # passport-local OR passport-google-oauth20
@nestjs/jwt \
passport-jwt
npm install --save-dev \
@types/passport-local \
@types/passport-jwt
# Hashing passwords?
npm install \
bcrypt
npm i --save-dev \
@types/bcrypt

Install winston dependencies

npm install \
  nest-winston \
  winston

Configure logging

main.ts

import { AppModule } from './app.module';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';

const app = await NestFactory.create(AppModule);
app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));

app.module.ts

import { WinstonModule } from 'nest-winston';
import * as winston from 'winston';

@Module({
  imports: [
    WinstonModule.forRoot({
      transports: [
        new winston.transports.Console({
          format: winston.format.combine(
            winston.format.timestamp({ format: 'MM-DD-YYYY HH:mm:ss.SSS' }),
            winston.format.align(),
            winston.format.printf((info) => {
              const ctx = info.context ? ` ${info.context} -` : '';
              return `[${info.level.substring(0, 1).toUpperCase()}] ${
                info.timestamp
              }${ctx} ${info.message}`;
            }),
          ),
        }),
      ],
      level: 'debug',
    }),
    // other modules...
  ],
})
export class AppModule {}

Usage

@Injectable()
export class MyService {
  private readonly logger = new Logger(MyService.name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment