This is a sample application for upload files with Nest Framework.
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.
$ npm install
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=
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
# 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
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;
CASE: always show 500 Internal Server Error
May be can't access to upload directory, run:
$ sudo chown -R [current_user] [project]
Client app that support this api: https://nextjs-upload-file-gules.vercel.app