๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Develop/Node

[nestjs] How to return image from external API using NestJs? ์ •์  ํŒŒ์ผ ์ œ๊ณต

by 3-stack 2022. 1. 16.

๐Ÿ“‘ [ ์ƒํ™ฉ ]

nestjs ๋ฅผ ์‚ฌ์šฉํ•ด์„œ API ์„œ๋ฒ„๋ฅผ ๊ฐœ๋ฐœํ•˜๋˜ ์ค‘

์ด๋ฏธ์ง€ ํŒŒ์ผ๋“ค์„ ์ •์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์—์„œ ์‚ฝ์งˆ์ด ์‹œ์ž‘๋๋‹ค.

์ด๋ฏธ์ง€ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ธŒ๋ผ์šฐ์ € ์ƒ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ๋„์šฐ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

 

1. ํ”„๋กœ์ ํŠธ ํด๋” ๊ตฌ์กฐ

files/images ํ•˜์œ„์˜ ํŒŒ์ผ์„ ์ œ๊ณตํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

 

2. ๊ณต์‹๋ฌธ์„œ ๋ฐฉ๋ฒ•

nest ๊ณต์‹๋ฌธ์„œ์—์„œ ์•ˆ๋‚ดํ•˜๋Š” ์ •์ ํŒŒ์ผ ์ œ๊ณต ๋ฐฉ๋ฒ•์ด๋‹ค.

@nestjs/serve-static ๋ผ์ด๋ธŒ๋Ÿฌ๋ฅผ ์„ค์น˜ํ•˜๊ณ , ServeStaticModule ์„ค์ •์„ ํ†ตํ•ด ์ •์ ํŒŒ์ผ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

npm install --save @nestjs/serve-static
import { join } from 'path';
import { Module } from '@nestjs/common';
import { ServeStaticModule } from '@nestjs/serve-static';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'client'),
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

 

๐Ÿ˜ก [ ๋ฌธ์ œ ]

๊ณต์‹๋ฌธ์„œ์™€ ๋™์ผํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ServerStaticModule ์„ค์ •ํ–ˆ์ง€๋งŒ ์•ˆ ๋œ๋‹ค!! ๐Ÿ˜…

@Module({
  imports: [
    ServeStaticModule.forRoot({
      rootPath: path.join(__dirname, '../..', 'files'),
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

 

`rootPath`๋กœ ์„ค์ •ํ•œ URL(C:\Users\superpower\dev\nest-001\files)๋Š” ์ •ํ™•ํ•˜๊ฒŒ ๊ฐ€๋ฅดํ‚ค๊ณ  ์žˆ๋‹ค.

URI๋กœ ๊ฐ€๋ฅดํ‚ค๋Š” zz2.png ํŒŒ์ผ์ด ์•„๋‹ˆ๋ผ index.html ํŒŒ์ผ๋กœ ์ฐพ์•„๊ฐ„๋‹ค.

[Nest] 14668  - 2022. 01. 16. ์˜คํ›„ 3:15:37   ERROR [ExceptionsHandler] ENOENT: no such file
  or directory, stat 'C:\Users\superpower\dev\nest-001\files\index.html'
Error: ENOENT: no such file or directory, stat 'C:\Users\superpower\dev\nest-001\files\index.html'
GET /api/images/zz2.png 404 2.071 ms - 125

 

๐Ÿงช [ ์›์ธ ]

  • @nestjs/serve-static ํŒจํ‚ค์ง€ ๊ณต์‹๋ฌธ์„œ๋ฅผ ๋ชจ๋‘ ํ™•์ธํ•˜๊ณ  ์„ค์ •๊ฐ’์„ ์ด๋ ‡๊ฒŒ ์ €๋ ‡๊ฒŒ ์ ์šฉํ•ด๋„ ์•ˆ๋œ๋‹ค.
  • ๊ตฌ๊ธ€๋ง์—๋„ @nestjs/serve-static <- ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํ•ด๊ฒฐ๋œ ์ด์Šˆ๊ฐ€ ์—†๋‹ค.
  • ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” SPA ํ”„๋กœ์ ํŠธ์˜ ๋นŒ๋“œ ํŒŒ์ผ๋งŒ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‚˜ ๋ณด๋‹ค.(์ฐธ๊ณ )
In order to serve static content like a Single Page Application (SPA) we can use the ServeStaticModule from the @nestjs/serve-static package.

 

๐ŸŽ‰ [ ํ•ด๊ฒฐ ]

Nest, by default, makes use of the Express library under the hood. Hence, every technique for using the MVC (Model-View-Controller) pattern in Express applies to Nest as well.

Model-View-Controller ํŒจํ„ด์˜ View ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด์„œ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ์ž.(์ฐธ๊ณ )

ํ”„๋กœ์ ํŠธ์˜ ๋ฃจํŠธํŒŒ์ผ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด useStaticAssets ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

ํ•ด๋‹น ๊ธฐ๋Šฅ์€ @nestjs/platform-express ์— ๋‚ด์žฅ๋˜์–ด ์žˆ์–ด ๋ณ„๋„์˜ ์„ค์น˜๋Š” ํ•„์š”์—†๋‹ค.

const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.useStaticAssets(path.join(__dirname, '../..', 'files'), {
  prefix: `/${GLOBAL_PREFIX}/`,
});

prefix ์˜ต์…˜์€ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ž…๋ ฅํ•˜๋Š” URL์˜ prefix ์„ค์ •์„ ์˜๋ฏธํ•œ๋‹ค.

app.setGlobalPrefix(GLOBAL_PREFIX); ๋ฅผ ํ•ด์คฌ์ง€๋งŒ ๋ณ„๋„๋กœ ์„ค์ •ํ•ด์•ผ ์ ์šฉ์ด ๋œ๋‹ค.

 

๐Ÿค—๐Ÿค—๐Ÿค—๐Ÿค—๐Ÿค— ์ž˜ ๋‚˜์˜จ๋‹ค. ๐Ÿค—๐Ÿค—๐Ÿค—๐Ÿค—

 

'Develop > Node' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

node-gyp rebuild fails during npm install canvas  (0) 2022.10.23
express.urlencoded ๊ฐ€ ๋ญ์ง€?  (0) 2022.01.16

๋Œ“๊ธ€