Reputation: 35
I use ServeStaticModule from '@nestjs/serve-static' to serve static images that located in "static" directory. When i try to access the image on "localhost:5000/image/[image name]" i get this message: {"statusCode":404,"message":"ENOENT: no such file or directory, stat '/Desktop/App/server/dist/static/index.html'"}. I follow official documentation.
import { Module } from '@nestjs/common';
import { FileModule } from './file/file.module';
import { TrackModule } from './track/track.module';
import { MongooseModule } from '@nestjs/mongoose';
import { ServeStaticModule } from '@nestjs/serve-static';
import { resolve } from 'path';
@Module({
imports: [
ServeStaticModule.forRoot({
rootPath: resolve(__dirname, 'static'),
}),
TrackModule,
FileModule,
],
})
export class AppModule {}
How to serve static files like images properly ?
Upvotes: 3
Views: 2896
Reputation: 56
This is my solution:
Set serveStaticOptions: { index: false } is your friend ;-D
// in nest-cli.json
"sourceRoot": "src",
"compilerOptions": {
...
"assets": [{ "include": "static/testdata/**/*", "watchAssets": true }]
}
// in app.module.ts
ServeStaticModule.forRoot({
rootPath: join(__dirname, '..', '/static/'),
serveStaticOptions: { index: false },
}),
serves images from src/static/testdata as
http://localhost:8080/testdata/image.png
Upvotes: 2