簡體   English   中英

如何在 Nestjs 中使用 ServeStaticModule 訪問多個子文件夾?

[英]How to access multiple sub folders with ServeStaticModule in Nestjs?

我有兩個不同的模塊用於PostsProfile的圖像。所以我可以將圖像保存在資產的兩個不同子文件夾中。

我的文件夾結構

在此處輸入圖像描述

現在的問題是我可以像這樣訪問谷歌上的posts文件夾圖像http://localhost:3000/47b7a6b9c10.jpg

但是當我嘗試訪問這樣的配置文件文件夾圖像時http://localhost:3000/93d1a567c5.png我得到了空白頁。 而且看不到圖。

app.module.ts 中的當前代碼

 PostsModule,
    ProfileModule,

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

我試過的東西

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'),
    }),

使用我的當前代碼,我可以訪問最初存在的子文件夾。 就像如果我用assets/profile替換assets/posts我可以訪問個人資料圖片,反之亦然

我已經找到適合我的特殊情況的解決方案。 術語renderPath了作用。

解決方案

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

要獲得有關其他ServeStaticModule API SPEC optionsproperties的更多信息,請訪問https://www.npmjs.com/package/@nestjs/serve-static

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM