kanokpit-yowaratch / nestjs-upload-api

NestJS API for Uploading files

Home Page:https://seer-of-human.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NestJS Upload API

Description

This is a sample application for upload files with Nest Framework.

Configuration

The configuration happens in multerOptions.

In the project the following options are used:

  • File size limit
  • Check mimetype
  • Storage destination is files/ folder
  • The original file name is overwritten with a uuid
  • More options are available. See multer documentation.

Installation

$ npm install

Setting environment variable

Install dotenv

Add require('dotenv').config(); in app.module.ts

Create .env file and set variables

API_PORT=[YOUR_PORT]
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=
DB_PASSWORD=
DB_DATABASE=

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

PM2 command

# build to create dist/main.js
$ npm run build

# pm2 to start api service
# first time
$ pm2 start dist/main.js --name "upload-api"
# next time
$ pm2 restart upload-api

DB Structure Example

CREATE TABLE `medias` (
	`image_code` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
	`source` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
	`file_name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
	`active_status` INT(10) NOT NULL,
	PRIMARY KEY (`image_code`) USING BTREE
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB;

Important config on NGINX

CASE: always show 500 Internal Server Error

May be can't access to upload directory, run:

$ sudo chown -R [current_user] [project]

Demo

https://seer-of-human.com

Client app that support this api: https://nextjs-upload-file-gules.vercel.app

About

NestJS API for Uploading files

https://seer-of-human.com


Languages

Language:TypeScript 95.4%Language:JavaScript 4.6%