Skip to content

Modern NestJs BE with both REST and GraphQL, Apollo Server with PubSub, Passport Auth, MongoDB, Redis. Deployment with Cloudfront, AWS CodePipeline and EC2

Notifications You must be signed in to change notification settings

thangtran3112/chatter-backend

Repository files navigation

Chatter backend with Apollo PubSub and Redis PubSub

  • For Production solution, i used Redis, please see the list event-publishing system
  • In this solution, we are using Redis PubSub for AWS Production server
  • On Local server, we are still using the normal Apollo PubSub comes with NESTJS. See pubsub.module.ts
  • By default, Javascript objects are serialized using the JSON.stringify and JSON.parse methods. This means that not all objects - such as Date or Regexp objects - will deserialize correctly without a custom reviver, that work out of the box with the default in-memory implementation. In our case, ObjectId, Date in MessageDocument will need a custom reviver.
  • Notes: The data we receive from Redis PubSub will need to be deserialized to JSON format in message.service.ts, before the resolvers can handle it. For this we are using custom-reviver

Install nest config and Mongo DB connection

NodeJS v20X npm i @nestjs/config @nestjs/mongoose mongoose npm i joi

Installing local Mongo DB

https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/

brew tap mongodb/brew brew update brew install mongodb-community@7.0

Using Mongo DB

NestJs GraphQL

  • Installing: npm i @nestjs/graphql @nestjs/apollo @apollo/server graphql
  • Playground GraphQL server, after starting backend: http://localhost:3001/graphql
  • Understand NestJS GraphQL: https://docs.nestjs.com/graphql/quick-start

Running MongoDB Compass to view the MongoDB

https://www.mongodb.com/try/download/compass https://www.mongodb.com/docs/compass/current/query/filter/?utm_source=compass&utm_medium=product

Using migrate-mongo package to manage DB migration

npm i migrate-mongo npm i --save-dev @types/migrate-mongo

Install auth module with @nest/passport

nest g module auth nest g service auth

Decoding the retrieved Authentication cookie from jwt.io

Install common practices Field Validation and annotate email and password

npm i --save class-validator class-transformer ValidationPipe

AWS Backend deployment with self-signed SSL certificate

openssl version
openssl genrsa 2048 > privatekey.pem
stat privatekey.pem

openssl req -new -key privatekey.pem -out csr.pem

Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:Washington
Locality Name (eg, city) []:Seattle
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Example Corporation
Organizational Unit Name (eg, section) []:Marketing
Common Name (e.g. server FQDN or YOUR name) []:prod.eba-cnqkbmvr.us-west-2.elasticbeanstalk.com
Email Address []:thangtran3112@gmail.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:sometestpassword
An optional company name []:

openssl x509 -req -days 365 -in csr.pem -signkey privatekey.pem -out public.crt

  • Upload public.crt to AWS Beanstalk with AWS CLI brew install awscli aws --version aws configure
AWS Access Key ID [None]: ************************
AWS Secret Access Key [None]: *************************************
Default region name [None]: us-west-2
Default output format [None]:

aws iam upload-server-certificate --server-certificate-name elastic-beanstalk-x509 --certificate-body file://public.crt --private-key file://privatekey.pem

Enable CORS on NESTJS backend

  • Enable CORS in main.ts through app.enableCors();
  • Enable CORS for GraphQLModule inside app.module.ts through cors: true

Backend Setting Cookie issue

  • By default, AWS does not allow setting cookie with different CORS origin

About

Modern NestJs BE with both REST and GraphQL, Apollo Server with PubSub, Passport Auth, MongoDB, Redis. Deployment with Cloudfront, AWS CodePipeline and EC2

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published