A progressive Node.js framework for building efficient and scalable server-side applications.
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.
$ yarn
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.
##########################################################
# 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 filew3s.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
andw3s
directories in theshared
folder and update the files as follows:
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 { }
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
}
}
}
# development
$ yarn start
After running the app, open your browser and navigate to http://localhost:8080/docs to view the Swagger documentation.