[英]How to access multiple sub folders with ServeStaticModule in Nestjs?
我有兩個不同的模塊用於Posts和Profile的圖像。所以我可以將圖像保存在資產的兩個不同子文件夾中。
我的文件夾結構
現在的問題是我可以像這樣訪問谷歌上的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 options
和properties
的更多信息,請訪問https://www.npmjs.com/package/@nestjs/serve-static
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.