简体   繁体   中英

How to access multiple sub folders with ServeStaticModule in Nestjs?

I have two different modules for images of Posts and Profile .So I can save the images in two different sub folders of assets.

My folder structure

在此处输入图像描述

Now the issue is I can access the posts folder images on google like this http://localhost:3000/47b7a6b9c10.jpg

but when I try to access the profile folder images like this http://localhost:3000/93d1a567c5.png I got blank page. And can't see the image.

The Current Code In app.module.ts

 PostsModule,
    ProfileModule,

    ServeStaticModule.forRoot(
      {
        rootPath: join(__dirname, '..', 'assets/posts'),
      },
      {
        rootPath: join(__dirname, '..', 'assets/profile'),
      },
    ),

Things that I tried with

1-

ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'assets/posts'),
    }),

ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'assets/profile'),
    }),

2-

    ServeStaticModule.forRoot(
      {
        serveRoot: '/assets',
        rootPath: join(__dirname, '..', '/posts'),
      },
      {
        serveRoot: '/assets',
        rootPath: join(__dirname, '..', '/profile'),
      },
    ),

3-

    ServeStaticModule.forRoot({
      serveRoot: '/assets',
      rootPath: join(__dirname, '..', '/posts'),
    }),

    ServeStaticModule.forRoot({
      serveRoot: '/assets',
      rootPath: join(__dirname, '..', '/profile'),
    }),

With My Current Code I can access the sub folder that is present at first. Like If I replace assets/posts with assets/profile I can access profile images and vice versa

I've found the solution for my particular situation. The term renderPath did the trick.

Sloution

ServeStaticModule.forRoot(
      {
        rootPath: join(__dirname, '..', 'assets/posts'),
        renderPath: '/posts',
      },
      {
        rootPath: join(__dirname, '..', 'assets/profile'),
        renderPath: '/profile',
      },
    ),

To get further information about other ServeStaticModule API SPEC options and properties visit https://www.npmjs.com/package/@nestjs/serve-static

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM