NestJS – How to use .env variables in main app module file for database connection

From Nestjs docs here – https://docs.nestjs.com/techniques/configuration These steps worked for me with MySQL and TypeORM. Install Nestjs config module – npm i –save @nestjs/config. It relies on dotenv Create a .env file in your root folder and add your key/value pairs e.g. DATABASE_USER=myusername Open app.module.ts and import the config module import { ConfigModule } from … Read more

How to use query parameters in Nest.js?

Query parameters You have to remove :params for it to work as expected: @Get(‘findByFilter’) async findByFilter(@Query() query): Promise<Article[]> { // … } Path parameters The :param syntax is for path parameters and matches any string on a path: @Get(‘products/:id’) getProduct(@Param(‘id’) id) { matches the routes localhost:3000/products/1 localhost:3000/products/2abc // … Route wildcards To match multiple endpoints … Read more

nestjs vs plain express performance

UPDATE – 17.03.2020 We are now running benchmarks for every new PR. One of the latest benchmarks can be found here: https://github.com/nestjs/nest/runs/482105333 Req/sec Trans/sec Nest-Express 15370 3.17MB Nest-Fastify 30001 4.38MB Express 17208 3.53MB Fastify 33578 4.87MB That means Nest + FastifyAdapter is now almost 2 times faster than express. UPDATE – 22.09.2018 Benchmarks directory has … Read more

What’s the difference between Interceptor vs Middleware vs Filter in Nest.js?

As you already implied with your question, all three are very similar concepts and in a lot of cases it is hard to decide and comes down to your preferences. But I can give an overview of the differences: Interceptors Interceptors have access to response/request before and after the route handler is called. Registration Directly … Read more

TypeORM Entity in NESTJS – Cannot use import statement outside a module

My assumption is that you have a TypeormModule configuration with an entities property that looks like this: entities: [‘src/**/*.entity.{ts,js}’] or like entities: [‘../**/*.entity.{ts,js}’] The error you are getting is because you are attempting to import a ts file in a js context. So long as you aren’t using webpack you can use this instead so … Read more

Inject nestjs service from another module

You have to export the ItemsService in the module that provides it: @Module({ controllers: [ItemsController], providers: [ItemsService], exports: [ItemsService] ^^^^^^^^^^^^^^^^^^^^^^^ }) export class ItemsModule {} and then import the exporting module in the module that uses the service: @Module({ controllers: [PlayersController], providers: [PlayersService], imports: [ItemsModule] ^^^^^^^^^^^^^^^^^^^^^^ }) export class PlayersModule {} ⚠️ Don’t add the … Read more