Skip to content

Example for setting up Swagger, CustomExceptionFilter with Fastify, uploading images to S3 and Web3 Storage, and resolving ES Module issues in NestJS

License

Notifications You must be signed in to change notification settings

ChroII0/nestjs-swagger-fastify-example

Repository files navigation

Nestjs-swagger-fastify-example

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 Support us

Description

A simple NestJS backend demo repository showcasing how to set up Swagger with Fastify, upload images to AWS S3 and Web3 Storage, and fix the ES Module require() issue in a NestJS application.

Installation

$ yarn

Environment Variables

Before running the application, make sure to configure your environment variables. You can create a .env file in the root of your project and populate it with the following variables. You can use the provided .env.development file as a template.

Example .env Configuration

##########################################################
# MONGODB CONFIG
##########################################################
MONGODB_URI=

##########################################################
# W3S CONFIG
##########################################################
W3S_KEY=
W3S_GATEWAY_URL=https://w3s.link/ipfs/

##########################################################
# AWS CONFIG
##########################################################
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=
S3_BUCKET_NAME=

##########################################################
# LOG4JS CONFIG
##########################################################
LOG_DIR=./logs

##########################################################
# CORS CONFIG
##########################################################
CORS_ORIGIN_LIST=http://localhost:3000,http://localhost:3001

##########################################################
# FILE CONFIG
##########################################################
UPLOAD_FILE_SIZE_LIMIT=10485760 # 1024 * 1024 * 10

Make sure to fill in the values for each variable according to your environment setup.

Note:

  • If you want to upload to Web3 Storage, you need to include W3S_KEY in your .env file and ensure that the file w3s.proof is located in the root directory.

  • If you do not need to use the S3 module or the W3S module, you can delete the s3 and w3s directories in the shared folder and update the files as follows:

Updated static.module.ts

import { Global, Module } from '@nestjs/common';
import { LogModule } from '@/shared/log/log.module';
import { MongooseModule } from '@nestjs/mongoose';
import { StaticController } from './static.controller';
import { StaticFile, StaticFileSchema } from './dao/static.entity';
import { StaticService } from './static.service';
import { StaticFileDAO } from './dao/static.dao';

@Global()
@Module({
  imports: [
    MongooseModule.forFeature([{ name: StaticFile.name, schema: StaticFileSchema }]),
    LogModule,
  ],
  controllers: [StaticController],
  providers: [StaticService, StaticFileDAO],
  exports: [StaticService]
})
export class StaticModule { }

Updated static.service.ts

import { Injectable } from '@nestjs/common';
import { StaticFileDAO } from './dao/static.dao';
import { FileUploadResponseDto } from '@/common/dto/file-upload-response.dto';

@Injectable()
export class StaticService {

    constructor(
        private readonly staticFileDAO: StaticFileDAO
    ) { }

    async uploadFile(
        fileToBuffer: Buffer,
        fileName: string,
        fileMimeType: string
    ): Promise<FileUploadResponseDto> {
        
        const w3sCid = "example w3sCid";

        const s3Url = "example s3Url"

        await this.staticFileDAO.updateAndSave({ s3Url, w3sCid }, {
            s3Url,
            w3sCid,
            createdAt: new Date(),
            updateAt: new Date()
        });

        return {
            ipfs: {
                baseUrl: "example baseUrl",
                cid: w3sCid
            },
            s3Url
        }
    }
}

Running the app

# development
$ yarn start

After running the app, open your browser and navigate to http://localhost:8080/docs to view the Swagger documentation.

License

MIT licensed.

About

Example for setting up Swagger, CustomExceptionFilter with Fastify, uploading images to S3 and Web3 Storage, and resolving ES Module issues in NestJS

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published