반응형
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);
});
}
반응형
'Programming > Node.js' 카테고리의 다른 글
[NestJS] create excel download by exceljs (2) | 2024.12.06 |
---|---|
[NestJS] read excel file With exceljs (DiskStorage) (0) | 2024.12.06 |
[NestJS] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client (0) | 2024.12.02 |
[NestJS] Handlebars template in hbs(Handlebars) (0) | 2024.11.26 |
[NestJS] Table leftJoin with Mysql2 (0) | 2024.11.24 |