Programming/Node.js
[NestJS] Image file download with Url
생각하는로뎅
2024. 12. 6. 03:08
반응형
1. npn install
npm i --save @nestjs/axios axios
2.Model 에 HttpModule 추가
import { HttpModule } from '@nestjs/axios';
@Module({
imports: [
HttpModule
]
3. 다운로드를 구현할 Service 에서 아래 코드를 추가하여 다운로드
import { HttpService } from '@nestjs/axios';
@Injectable()
export class ApiService {
constructor(
, private readonly httpService : HttpService
...
..
async downLoadImage() {
const writer = fs.createWriteStream('저장할 파일 경로');
const response = await this.httpService.axiosRef({
url: 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png',
method: 'GET',
responseType: 'stream',
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}
반응형