Skip to content

Creating Simple Contact API with Nest.js. including multi-role authentication, mail sender implementation, and use cloud storage for saving image file

Notifications You must be signed in to change notification settings

hafizcode02/nest-simple-api

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Donate us Support us Follow us on Twitter

Description

My Learning Project in Nest.JS. Creating an Simple Contact API, this project including Authentication based on stored token on database and automatically logout if token is expired, specify which role that can accessed some routes (can be spesifically access by 1 role or multiple role), automatically sending verification email after register and after verifed, setup configuration for using multer uploads and save the file into local, or famous cloud storage (Cloudflare R2, AWS S3, Google Cloud Storage).

Project Checklist

User Endpoint

  • Register User
  • Send Email to Verify User
  • Verify User
  • Send Welcome Email after User Verified.
  • Login User
  • Get Logged In User
  • Update User Data
  • Logout User

Contact Endpoint

  • Get All Contact & Search Contact
  • Create Contact
  • Get Contact
  • Update Contact
  • Update Contact Image
  • Delete Contact

Address Endpoint

  • Get All Addresses in the Contact
  • Create Addresses of Contact
  • Get Address
  • Update Address
  • Delete Address

Todo

  • Make Endpoint for forgot password
  • Setup Swagger UI
  • Create setup for multer to upload in cloud bucket like Cloudflare R2/AWS S3/GCP Cloud Storage

Project Endpoint

You can see at doc folder or import the postman collection. or you can go to swagger api docs:

SWAGGER API DOCS

Project setup

npm install
npx prisma init

Setup your .env

DATABASE_URL="mysql://root:@localhost:3306/nest_contacts?schema=public"

# MAIL CONFIG
MAIL_HOST="yourwebserver.domain.com"
MAIL_PORT=465
MAIL_SECURE=true
[email protected]
MAIL_PASS=YourPassword

# HASHIDS CONFIG
HASHIDS_SALT=YOURSALT

# STORAGE MODE
STORAGE=local # (local, r2, s3, gcp) : local (local server), r2 (cloudflare), s3 (aws), gcp(google cloud)

## LOCAL UPLOADS
UPLOAD_DIR=uploads # Directory for local uploads

## CLOUDFLARE R2 STORAGE
CLOUDFLARE_R2_ENDPOINT=https://<your-account-id>.r2.cloudflarestorage.com
CLOUDFLARE_R2_DEV_URL=<your-r2-dev-subdomain-public-url>
CLOUDFLARE_R2_ACCESS_KEY=<your-access-key-id>
CLOUDFLARE_R2_SECRET_KEY=<your-secret-access-key>
CLOUDFLARE_R2_BUCKET=<your-bucket-name>

## AWS S3 STORAGE
AWS_REGION=us-west-2  # Replace with your actual region
AWS_ACCESS_KEY_ID=your-access-key-id  # Replace with your actual access key ID
AWS_SECRET_ACCESS_KEY=your-secret-access-key  # Replace with your actual secret access key
AWS_BUCKET_NAME=your-s3-bucket-name  # Replace with your actual bucket name

GCP_PROJECT_ID=your-gcp-project-id
GCP_KEY_FILE=path/to/your/service-account-key.json
GCP_BUCKET_NAME=your-gcs-bucket-name

Run Prisma

npx prisma migrate dev

Compile and run the project

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Run tests

Make sure you change the email for testing in should be return 201 when request is valid and success register at user.spec.ts

# unit tests (empty record in db)
$ npm run test

# have record in db (run partial)
$ npm run test -- user.spec.ts
$ npm run test -- contact.spec.ts
$ npm run test -- address.spec.ts

Another way to Run

More simple shortcut to use in windows :D

run.bat dev|test|build|lint|prod

Deployment

When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the deployment documentation for more information.

If you are looking for a cloud-based platform to deploy your NestJS application, check out Mau, our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:

npm install -g mau
mau deploy

With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.

Resources

Check out a few resources that may come in handy when working with NestJS:

  • Visit the NestJS Documentation to learn more about the framework.
  • For questions and support, please visit our Discord channel.
  • To dive deeper and get more hands-on experience, check out our official video courses.
  • Deploy your application to AWS with the help of NestJS Mau in just a few clicks.
  • Visualize your application graph and interact with the NestJS application in real-time using NestJS Devtools.
  • Need help with your project (part-time to full-time)? Check out our official enterprise support.
  • To stay in the loop and get updates, follow us on X and LinkedIn.
  • Looking for a job, or have a job to offer? Check out our official Jobs board.

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch (Nest.Js Author)

License

Nest is MIT licensed.

About

Creating Simple Contact API with Nest.js. including multi-role authentication, mail sender implementation, and use cloud storage for saving image file

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages