Skip to main content

Posts

Showing posts with the label NestJS-Technology

Part-3 NestJS Email With File Attachment

In this article, we are going to implement NestJS sending emails with file attachments. Part-1 NestJS Send Plain Text Email Part-2 NestJS Send Dynamic Generated HTML Template As Email Install @types/nodemailer Package: Let's install the typescript nodemailer package. npm install --save-dev @types/nodemailer Endpoint To Send Email With File Attachment: Let's add a sample image file into the 'mail' folder that we are going to attach to the email. Let's create an endpoint to send the email with a file attachment. src/email.controller.cs: import { join } from 'path'; @Get('file-attachment') async fileAttachement(@Query('toemail') toemail){ var response = await this.mailService.sendMail({ to: toemail, from: 'nani.bommidi93@gmail.com', subject: 'File Attachment', html: "<h1>File Attachment</h1>", attachments:[{ path: join(__dirname,'mails','

Part-2 Generate Dynamic Email Template And Send Email In The NestJS Application

In this article, we are going to generate a dynamic data binding email template and then send emails from the NestJS application. Click here for part-1 Install Handlebar Package: Email body dynamic data binding can be achieved by using the 'Handlebar' libraries. npm install --save handlebars Register Handlebar Configuration: Let's register our handlebar template configuration into the 'MailerModule' in the 'AppModule'. src/app.module.ts: import { MailerModule } from '@nestjs-modules/mailer'; import { Module } from '@nestjs/common'; import { join } from 'path'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { EmailController } from './email.controller'; import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter'; @Module({ imports: [ MailerModule.forRoot({ transport: { host: 'smtp.send

Part-1 Email Sending In NestJS Application

In this article, we are going to understand email sending in the NestJS application. SMTP: Simple Mail Transfer Protocol(SMTP) is used to send, and receive emails in any application. So on an email sent, its transferred over the internet from one server to another using SMTP. For our demo, we are going to use the 'SendGrid' a third-party provider SMTP. SendGrid: To establish email communication in our NestJS application using SendGrid, then we need a few configurations from SendGrid like 'SMTP Host', 'from email address' 'user name', and 'password'. Step 1: Go to SendGrid's official website at 'https://sendgrid.com'. Next, do signup. Step 2: Now login into the SendGrid website, then under the left-hand side menu go to the 'Settings', then select 'Sender Authentication' menu. At 'Single Sender Verification' click on the 'Get Started' button. Step 3: Let's register the 'From Email Address'

Use Azure Cache For Redis In NestJS Application

In this article, we are going to do a small demo on the integration of Azure Redis Cache into the NestJS application. Redis Cache: Redis is an open-source in-memory data structure store, used as a database, cache. It supports data structures such as strings, hashes, lists, sets, stored sets with range queries, etc. Let's understand the flow of Redis cache in a web application: Users request a page from the web application. Web application queries the Redis store, if the data exists, then fetches the data and returns the response directly to the user. If no data is in the Redis store, the application queries the database and fetches the data, next save the same data into the Redis store so that subsequent user request can consume the data from the Redis store directly, finally returns the response to the users. Create A Azure Redis Cache: Step:1 Create and sign-in into the Azure portal(https://portal.azure.com/) Step:2 On the Azure portal home page click on 'Create a resourc

NestJS Application Using CQRS Design Pattern

In this article, we will implement the CQRS Design Pattern in the NestJS sample application. CQRS: CQRS stands for Command Query Responsibility Segregation. CQRS guides us to separate our logical implementation into 2 categories like 'Command', 'Query'. The 'Commands' specifies the operation like creation or updating of data into the data source(database). The 'Query' specifies the operations to fetch the data. PostgreSQL Database: For this demo, I'm using the free open-source PostgreSQL database. Here I'm going to use the PostgreSQL docker image because it is easy and fast to set up and configure.  Click here to getting started with PostgreSQL docker . Run the below command to create an example table for a demo like 'Person'. CREATE TABLE Person( Id SERIAL PRIMARY KEY NOT NULL, Name Text NULL, Age INT NULL ) Create A NestJS Application: Let's begin our demo by creating a sample NestJS application. Command To Install NestJS C

NestJS JWT Auth Cookie Series - Part-3 - Refresh Token

In the  previous article , we understand the steps to generate the JWT token and store it in the cookie. Now here we will understand steps to protect API and also about refresh token. Install passport-jwt NPM Package: We have to create a new jwt passport strategy to validate the jwt token, so we need to install the below packages. Command To Install passport-jwt Packages: npm install --save passport-jwt npm install --save-dev @types/passport-jwt Install And Setup Cookie Parser: To read the cookie in the nestjs application we have to install the below plugin. Command To Install Cookie Parser: $ npm i cookie-parser $ npm i -D @types/cookie-parser Now configure the cookie parser 'main.ts' src/main.ts: Create JWT Passport Strategy: So to apply authentication to API's we have to validate our jwt token, so to do that we need to create a new jwt passport strategy. src/users/jwt.strategy.ts: import { Injectable, UnauthorizedException } from "@nestjs/common"; im