file - 测试文件上传时如何修复 tsc-watch 重启?
问题描述
我想在我的 NestJS 项目中本地测试文件上传 API。我已经关注了这个文档。但是,当我上传文件时,tsc-watch
由于upload
文件夹已更改,因此会重新启动。
在这里,我使用的是NestJS
版本 6 以及从nest-cli
. 我也在使用谷歌云存储来存储文件。我通过运行在本地测试项目yarn start:dev
。
我已经尝试在两者的选项中添加upload
文件夹,但它不起作用。exclude
tsconfig.json
tsconfig.build.json
我也尝试过使用nodemon
. 不幸的是,nodemon
当我更改代码时不会重新启动。
以下是一些代码(此处为repo):
控制器
@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"
},
...
}
我希望该文件之后会被上传和删除,但tsc-watch
会不断重新启动并且永远不会上传或删除文件。
更改目录时是否有停止tsc-watch
重新启动upload
的方法,或者我应该使用其他工具吗?
解决方案
使用的第一个解决方案ts-lint
是将文件夹移到upload
文件夹之外src
并忽略该文件夹。
另一种解决方案与您建议的相同,使用nodemon
with ts-node
,但nodemon
本身存在观看文件的问题docker
并将启动命令更改为以下内容。
"start": "nodemon -L --watch 'src/**/*.ts' --exec ts-node src/main.ts",
推荐阅读
- python - Django DRF - 通过权限限制对列表视图的访问
- redux - 如何为 reduxForm() 设置全局默认值
- node.js - 在 node/express.js 子应用之间共享视图和静态文件
- sql - 将 SQL Developer 连接到 Redshift
- java - JSF f:convertNumber 在面板中有效,但在对话框中无效
- sass - 如何通过 SASS 更改 Bootstrap 4 的边框底部宽度?
- javascript - 在 React Native 项目中,为什么在导入中将顶级目录称为“app”?
- git - 为什么詹金斯从下载的 git repo 中删除空目录?
- c# - 当多个客户端连接时,我的多线程在线服务器变得太慢,C#
- ios - 我正在尝试将命令从 SKScene(SpriteKit) 传递到 SCNScene(SceneKit)