nestjs

How do I pass plain text as my request body using NestJS?

橙三吉。 提交于 2019-12-07 16:31:29
问题 One of the controller methods in my NestJS application is supposed to take plain text as its body but whenever I try to make a request, the parameter is received as an empty object. Is this even possible or am I going to have to create some sort of DTO to pass that single string? Example: @Post() myFunction(@Body() id: string) { // do something here } 回答1: I see that this question is pretty old, but it is listed in google among first, so I want to add answer here. If you don't want to add

NestJS Authentication with Auth0 via `passport-jwt`

寵の児 提交于 2019-12-07 07:44:50
问题 I'm trying to create a NestJS project that uses Auth0 for authentication, with the passport-jwt library (in conjunction with @nestjs/passport ), though I am unable to get it to work. I'm not sure where I'm going wrong. I've read the docs over and over again but still can't find the problem. Code /src/auth/jwt.strategy.ts import { Injectable, UnauthorizedException } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt';

How to handle mongoose error with nestjs

戏子无情 提交于 2019-12-07 00:19:34
I followed the example from https://docs.nestjs.com/techniques/mongodb The issue is when there is a mongoose validation error (e.g i have a schema with a required field and it isn't provided): From games.service.ts: async create(createGameDto: CreateGameDto): Promise<IGame> { const createdGame = new this.gameModel(createGameDto); return await createdGame.save(); } The save() function returns a Promise. Now i have this in the game.controller.ts @Post() async create(@Body() createGameDto: CreateGameDto) { this.gamesService.create(createGameDto); } What is the best way to handle an error and then

How can i setup multitenant in NESTJS

爷,独闯天下 提交于 2019-12-06 11:07:19
I want to connect to any database based on the subdomain (multi-tenant), but i'm not sure how can i do it. My code runs when the app is started, but i don't know how to change the Datasource based on subdomain. PS: I created middleware on each request, but I don't know how to change the source. I have the following code for my DB: import { connect, createConnection } from 'mongoose'; import { SERVER_CONFIG, DB_CONNECTION_TOKEN } from '../server.constants'; const opts = { useCreateIndex: true, useNewUrlParser: true, keepAlive: true, socketTimeoutMS: 30000, poolSize: 100, reconnectTries: Number

Defining Node environment in Nest.js

你离开我真会死。 提交于 2019-12-06 11:03:15
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 the npm scripts (for example "start:dev": "NODE_ENV=development nodemon" ), but I'm wondering if there

What's a valid @MessagePattern for NestJS MQTT microservice?

假如想象 提交于 2019-12-06 07:43:17
问题 I'm trying to setup a MQTT Microservice using NestJS according to the docs. I've started a working Mosquitto Broker using Docker and verified it's operability using various MQTT clients. Now, when I start the NestJS service it seems to be connecting correctly (mqqt.fx shows new client), yet I am unable to receive any messages in my controllers. This is my bootstrapping, just like in the docs: main.ts async function bootstrap() { const app = await NestFactory.createMicroservice(AppModule, {

Error while running nestjs in production mode, cannot find module

浪尽此生 提交于 2019-12-05 21:22:18
问题 I have implemented a generic class as below which might be causing the problem, import { Logger } from '@nestjs/common'; import { PaginationOptionsInterface, Pagination } from './paginate'; import { Repository } from 'typeorm'; export class EntityService<T> { private repository: Repository<T>; constructor(repository) { this.repository = repository; } async getEntityWithPagination( options: PaginationOptionsInterface, ): Promise<Pagination<T>> { const [results, total] = await this.repository

How do I pass plain text as my request body using NestJS?

随声附和 提交于 2019-12-05 20:37:07
One of the controller methods in my NestJS application is supposed to take plain text as its body but whenever I try to make a request, the parameter is received as an empty object. Is this even possible or am I going to have to create some sort of DTO to pass that single string? Example: @Post() myFunction(@Body() id: string) { // do something here } I see that this question is pretty old, but it is listed in google among first, so I want to add answer here. If you don't want to add body-parser middleware (for example, you want plain text only in single controller method), you can use raw

NestJS Authentication with Auth0 via `passport-jwt`

会有一股神秘感。 提交于 2019-12-05 19:16:05
I'm trying to create a NestJS project that uses Auth0 for authentication, with the passport-jwt library (in conjunction with @nestjs/passport ), though I am unable to get it to work. I'm not sure where I'm going wrong. I've read the docs over and over again but still can't find the problem. Code /src/auth/jwt.strategy.ts import { Injectable, UnauthorizedException } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { passportJwtSecret } from 'jwks-rsa'; import { xor } from 'lodash'; import { JwtPayload } from

NestJS + TypeORM: Use two or more databases?

不羁岁月 提交于 2019-12-05 04:50:21
I'm trying since 2 days to solve this, perhaps I'm simply missing the point here. My goal was to write a NestJS app (with TypeORM included) which serves a RestAPI for 2 or 3 of my little projects, instead of writing a NestJS-App for every single one of them. So far so good, the app is ready, works well with the single projects (which resides in subfolders with their entities, controllers, services, modules), but I can't get it to run with all of them. The point seems to be the configuration, I'm using ormconfig.json : [ { "name": "Project1", "type": "mysql", "host": "localhost", "port": 3306,