I want to test file uploading API locally in my NestJS project. I have followed this doc. However, when I uploaded files the tsc-watch
restart since the upload
folder is changed.
Here I am using NestJS
version 6 and everything installed from nest-cli
. I am also using Google Cloud Storage to store the files. I test the project locally by running yarn start:dev
.
I have tried adding upload
folder in exclude
option in both tsconfig.json
and tsconfig.build.json
but it does not work.
I have also tried using nodemon
. Unfortunately, nodemon
does not restart when I make change to the code.
Here are some codes (repo here):
@Patch(':id/file')
@UseInterceptors(
FileInterceptor('file', { dest: join(__dirname, '../../upload') }),
)
async uploadProblem(
@UploadedFile() file: FileDto,
@Param('id') id: string,
) {
if (file) {
await this.service.uploadProblem(id, file);
} else {
throw new BadRequestException('No files uploaded');
}
}
async uploadProblem(id: string, file: FileDto) {
const problem = await this.findById(id);
await this.fileService.uploadFile(`${problem.code}/problem`, file);
}
fileService
async uploadFile(name: string, file: FileDto) {
await this.bucket.upload(file.path, {
destination: name,
contentType: file.mimetype,
resumable: false,
});
removeSync(file.path);
}
package.json
{
...
"scripts": {
...
"start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \"node dist/main.js\"",
...
},
"dependencies": {
"@google-cloud/storage": "^3.3.1",
"@nestjs/common": "^6.0.0",
"@nestjs/core": "^6.0.0",
"@nestjs/mongoose": "^6.1.2",
"@nestjs/platform-express": "^6.0.0",
"class-transformer": "^0.2.3",
"class-validator": "^0.10.1",
"fs-extra": "^8.1.0",
"mongoose": "^5.7.3",
"reflect-metadata": "^0.1.12",
"rimraf": "^2.6.2",
"rxjs": "^6.3.3"
},
"devDependencies": {
"@nestjs/testing": "^6.0.0",
"@types/express": "4.16.1",
"@types/fs-extra": "^8.0.0",
"@types/jest": "24.0.11",
"@types/mongoose": "^5.5.19",
"@types/node": "11.13.4",
"@types/supertest": "2.0.7",
"jest": "24.7.1",
"prettier": "1.17.0",
"supertest": "4.0.2",
"ts-jest": "24.0.2",
"ts-node": "8.1.0",
"tsc-watch": "2.2.1",
"tsconfig-paths": "3.8.0",
"tslint": "5.16.0",
"typescript": "3.4.3"
},
...
}
I expect the file to be uploaded and deleted afterward, but tsc-watch
keeps restarting and never upload nor delete the file.
Is there anyway to stop tsc-watch
from restart when upload
directory is changed, or is there any other tools should I use?
The first solution, using ts-lint
is to move the upload
folder outside the src
folder and ignore that folder.
Another solution is the same as you suggest, using nodemon
with ts-node
, but nodemon
itself has the problem of watching files inside docker
and change start command to the following.
"start": "nodemon -L --watch 'src/**/*.ts' --exec ts-node src/main.ts",