gpt4 book ai didi

file - 上传测试文件时如何修复 tsc-watch 重启?

转载 作者:行者123 更新时间:2023-12-04 15:39:35 27 4
gpt4 key购买 nike

我想在我的 NestJS 项目中本地测试文件上传 API。我关注了this doc .但是,当我上传文件时,tsc-watch 重新启动,因为 upload 文件夹已更改。

在这里,我使用的是 NestJS 版本 6 以及从 nest-cli 安装的所有内容。我也在使用谷歌云存储来存储文件。我通过运行 yarn start:dev 在本地测试项目。

我尝试在 tsconfig.jsontsconfig.build.jsonexclude 选项中添加 upload 文件夹> 但它不起作用。

我也尝试过使用 nodemon。不幸的是,当我更改代码时,nodemon 没有重新启动。

这里有一些代码(repo here):

Controller

    @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);
}

文件服务

    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"
},
...
}

我希望文件会在之后上传和删除,但是 tsc-watch 一直重启并且永远不会上传或删除文件。

upload 目录更改时,有没有办法阻止 tsc-watch 重启,或者我应该使用其他工具吗?

最佳答案

第一个解决方案,使用 ts-lint 是将 upload 文件夹移动到 src 文件夹之外并忽略该文件夹。

另一种解决方案和你建议的一样,使用nodemonts-node,但是nodemon本身有watch里面文件的问题docker 并将启动命令更改为以下内容。

"start": "nodemon -L --watch 'src/**/*.ts' --exec ts-node src/main.ts",

关于file - 上传测试文件时如何修复 tsc-watch 重启?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58360593/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com