How to create nested routes with parameters using NestJS

i think you need this? import {Controller, Get, Param} from “@nestjs/common”; @Controller(‘accounts/:account’) export class TestController{ @Get(‘resource2/:someParam/whatever’) arsPW(@Param(‘account’) account, @Param(‘someParam’) someparam){ console.log(‘:account/resource2/:someParam/whatever’,account,someparam) return account+’_’+someparam+’___’; } @Get(‘resource1/:someparam’) aRSP(@Param(‘account’) account, @Param(‘someparam’) someparam){ console.log(‘:account/resource1/:someParam’,account,someparam) return account+’_’+someparam; } @Get() getget(){ console.log(‘get’); return ‘aaa’; } }

How to use Nest.js’s @Headers properly?

Headers will be sent in lower case so you need @Headers(‘my-id’) instead. Easy to debug by injecting the full headers object: import { Headers } from ‘@nestjs/common’; … @Put(“https://stackoverflow.com/”) public async put(@Headers() headers) { console.log(headers); } The headers variable will then refer to the req.headers.

In Nest.js, how to get a service instance inside a decorator?

Late to the party, but since I had a similar problem (Use global nest module in decorator) and stumbled upon this question. import { Inject } from ‘@nestjs/common’; export function yourDecorator() { const injectYourService = Inject(YourServiceClass); return (target: any, propertyKey: string, propertyDescriptor: PropertyDescriptor) => { // this is equivalent to have a constructor like constructor(yourservice: … Read more

Logging request/response in Nest.js

https://github.com/julien-sarazin/nest-playground/issues/1#issuecomment-682588094 You can use middleware for that. import { Injectable, NestMiddleware, Logger } from ‘@nestjs/common’; import { Request, Response, NextFunction } from ‘express’; @Injectable() export class AppLoggerMiddleware implements NestMiddleware { private logger = new Logger(‘HTTP’); use(request: Request, response: Response, next: NextFunction): void { const { ip, method, path: url } = request; const userAgent = … Read more

Class-validator – validate array of objects

Add @Type(() => AuthParam) to your array and it should be working. Type decorator is required for nested objects(arrays). Your code becomes import { IsArray, ValidateNested, ArrayMinSize, ArrayMaxSize } from ‘class-validator’; import { AuthParam } from ‘./authParam.model’; import { Type } from ‘class-transformer’; export class SignInModel { @IsArray() @ValidateNested({ each: true }) @ArrayMinSize(2) @ArrayMaxSize(2) @Type(() … Read more

Is there a recommended way to update NestJS?

You can use the Nest CLI to update the dependencies: $ npm install -g @nestjs/cli $ nest update You can also $ nest u As Mick mentioned in his comment, you might have to add –force argument. nest update –force Update – July 7 2022 Since v9.0.0 release, the command update was removed. To upgrade … Read more

Error while running nestjs in production mode, cannot find module

I have found the issue, it was because of absolute path while importing the class. import { EntityService } from ‘../shared/service-common’; //correct way import { EntityService } from ‘src/shared/service-common’; // wrong autoimport To fix auto import, I have added this setting in VS Code “typescript.preferences.importModuleSpecifier”: “relative”

Validate nested objects using class validator and nestjs

for me, I would able to validate nested object with ‘class-transformer’ import { Type } from ‘class-transformer’; full example: import { MinLength, MaxLength, IsNotEmpty, ValidateNested, IsDefined, IsNotEmptyObject, IsObject, IsString, } from ‘class-validator’; import { Type } from ‘class-transformer’; class MultiLanguageDTO { @IsString() @IsNotEmpty() @MinLength(4) @MaxLength(40) en: string; @IsString() @IsNotEmpty() @MinLength(4) @MaxLength(40) ar: string; } export … Read more