How to serve static images in NestJS How to serve static images in NestJS express express

How to serve static images in NestJS


This is what I've done:

  1. in app.module.ts

     import { ServeStaticModule } from '@nestjs/serve-static/dist/serve-static.module'; import { join } from 'path'; @Module({     imports: [         ServeStaticModule.forRoot({             rootPath: join(__dirname, '..', 'public'),         }),     ], })
  2. Then I created "public" dir on the same level as "src", "dir" etc.

  3. The file is available at: https://my-url/file.jpg


add this to main.tsreplace "uploads" with your directory nameapp.use('/uploads',express.static(join(process.cwd(), 'uploads')));


The idea of @pzabrocki brings me to the following minimal working repository: https://gitlab.com/hadrien-toma/nest-static-files

To play with it:

git clone https://gitlab.com/hadrien-toma/nest-static-filesyarn installyarn run start

Then the my-file.json is served at http://localhost:3333/my-file.json.