nestjs

Defining Node environment in Nest.js

两盒软妹~` 提交于 2020-01-02 17:12:30
问题 I'm in the process of setting up Nest.js project and I look for the efficient solution of defining Node environment which is used by the ConfigService for loading environment variables: import { Module } from '@nestjs/common'; import { ConfigService } from './config.service'; @Module({ providers: [ { provide: ConfigService, useValue: new ConfigService(`environments/${process.env.NODE_ENV}.env`) } ], exports: [ConfigService] }) export class ConfigModule {} Right now I'm defining it directly in

What's the difference between tsc (TypeScript compiler) and ts-node?

天大地大妈咪最大 提交于 2019-12-30 06:31:44
问题 I'm very confused about the difference between tsc and ts-node . I'm learning TypeScript and I usually transpile server .ts files with tsc command. Now, I'm approaching nestjs framework, and I see that it uses ts-node . So what's the difference between the two? Which one should I use? 回答1: The main difference is that tsc transpile all the file according to your tsconfig. Instead, ts-node will start from the entry file and transpile the file step by step through the tree based on the import

How to use nestjs Logging service

大城市里の小女人 提交于 2019-12-30 02:40:07
问题 I tried to use the internal Logger of nestjs (described on https://docs.nestjs.com/techniques/logger -> but with no description of how to use it) But I had problems (tried to inject LoggerService and so on) Can anybody explain how to do this? 回答1: Best practise Better than accessing the Logger statically is to create an instance for your class: @Controller() export class AppController { private readonly logger = new Logger(AppController.name); @Get() async get() { this.logger.log('Getting

Trying to understand models in a nestjs/mongoose app

心已入冬 提交于 2019-12-24 22:25:14
问题 I am a newbie to nestjs. I have now been playing with a very simple mongodb/mongoose db with books, authors and genres. I started to have a blurry image of what models I actually need for, let's say, books. Currently I have 2 models: book.ts export interface Book extends Document { id?: string; name: string; year?: string; authorId: string; genreId: string; } needed to inject into a service constructor @InjectModel('Book') private readonly bookModel: Model<Book> and to call book.save()

How to use new instance for every new HTTP request in NestJS?

喜欢而已 提交于 2019-12-24 14:43:11
问题 I have an API and was trying to send a request. That is working but I noticed that the classes were not destroyed after I received a response. I'm working with nestJS at the moment but nodeJS + expressJS also had this issue when I tried to test. I'm using following code: @Injectable() export class UsersService { s = ''; constructor() {} async findAll(): Promise<any> { this.s += ' haha '; return await this.s; } } This returned haha first time haha haha the second time and so on. I'm not really

NestJS How to configure middleware with async/await?

北城余情 提交于 2019-12-24 13:01:39
问题 I'm trying to use bull-arena in a NestJS app. export class AppModule { configure(consumer: MiddlewareConsumer) { const queues = this.createArenaQueues(); const arena = Arena({ queues }, { disableListen: true }); consumer.apply(arena).forRoutes('/system/queues'); } createArenaQueues() { return [ { name: 'Notification_Emailer', hostId: 'MyAwesomeQueues', url: 'redis://localhost', }, ]; } } This works! But I need to use async/await for createArenaQueues() , due to loading queues from db. export

nestjs intercept and modify outgoing http request

允我心安 提交于 2019-12-24 11:42:49
问题 So I'm likely missing something or doing something wrong. I have a NestJS application that is trying to make an http request to an external API. I'd like to be able to intercept this outgoing request and modify headers on it before executing it. I've tried using Interceptors to no avail, the incoming http requests get intercepted but not the outgoing. Any suggestions or help would be greatly appreciated. 回答1: Let's first deal with I've tried using Interceptors to no avail, the incoming http

NestJs TypeORM configuration using AWS Parameter Store

无人久伴 提交于 2019-12-24 07:45:25
问题 new to NestJS and have come across an issue. For our deployments we need to get our configuration from AWS Parameter Store (Systems Manager), including the database connection string. I have a ConfigModule and ConfigService which retrieves all the parameter store entries for my environment based on the param store path: Here is my config service: import * as dotenv from 'dotenv'; import * as fs from 'fs'; import * as AWS from 'aws-sdk'; export class ConfigService { private readonly envConfig:

how to use nestjs redis microservice?

牧云@^-^@ 提交于 2019-12-24 06:01:04
问题 I am learning nestjs microservice, What command can I use? const pattern = { cmd: 'get' }; this.client.send<any>(pattern, data) And how can I receive data from redis? constructor(private readonly appService: AppService) {} @Client({ transport: Transport.REDIS, options: { url: 'redis://127.0.0.1:6379', }, }) client: ClientProxy; @Get() getHello(): any { const pattern = { cmd: 'get foo' }; //Please write sample code here in document const data = ''; return this.client.send<any>(pattern, data);

NestJS websocket Broadcast event to clients

放肆的年华 提交于 2019-12-24 00:36:35
问题 I'm not able to send data from server NestJS to clients with a websocket. Nothing is emitted. My use case: several clients connected to a server with a websocket client send a message to server through the websocket server broadcast the message to all client My stack: NestJS server with websocket Angular client and other (like chrome extension for testing websockets) My code: simple-web-socket.gateway.ts: import { SubscribeMessage, WebSocketGateway, WsResponse, WebSocketServer,