Dimanoid
Dimanoid

Reputation: 7289

How to define path prefix in ServeStaticModule?

I want to serve some static directory structure according to this docs https://docs.nestjs.com/recipes/serve-static#serve-static but can find answers to these simple questions:

  1. How to set up path prefix like '/some/path/to/files'?
  2. How to set up serving at runtime (files path is dynamic and can't be hardcoded)?

Thanks in advance.

P.S. I saw some answers about app.useStaticAssets() but as far as I understood this is Express specific solution and wont compile with latest version anyway (Property 'useStaticAssets' does not exist on type 'INestApplication')...

Update

For example I have these directories:

  1. /var/lib/app/data1
  2. /var/lib/app/data2
  3. /home/user/some/path

And I want it to be served as:

  1. http://localhost:3000/path1
  2. http://localhost:3000/path333
  3. http://localhost:3000/my/funny/cats

Upvotes: 9

Views: 13613

Answers (5)

rajat
rajat

Reputation: 1

In case someone wants to get the static file root path from config service then we can do like this

ServeStaticModule.forRootAsync({
     imports: [ConfigModule],
     inject: [ConfigService],
     useFactory: (configService: ConfigService) => [
     {
       rootPath: configService.get<string>('rootPath'),
     },
   ],
})

Upvotes: 0

serv-inc
serv-inc

Reputation: 38267

You asked

How this can help me to serve many different folders from the different paths and some of them should be determined at runtime?

You can have several ServeRoot declarations, like so:

ServeStaticModule.forRoot({
  serveRoot: '/path1',
  rootPath: `/var/lib/app/data1`,
}),
ServeStaticModule.forRoot({
  serveRoot: '/path333',
  rootPath: `/var/lib/app/data2`,
}),
ServeStaticModule.forRoot({
  serveRoot: '/my/funny/cats',
  rootPath: `/home/user/some/path`,
}),

as for "determined at runtime", maybe a ServeStaticModule can not do this. Others should please correct me on that.

Upvotes: 5

HartWoom
HartWoom

Reputation: 587

This is an old question but I encountered it for the same problem.

Here is mine, so with the help of the answer of @Jay, which gives the link to all the options available on github, I was able to find the correct option, which is serveRoot when using NestJS ServeStaticModule.

Here is the use case where I used it :

ServeStaticModule.forRoot({rootPath: '/media/avatar', serveRoot: '/avatar'})

This line above is to include in your app.module in the import, basically I'm saying that files located in /media/avatar should be served on localhost:1234/avatar.

Upvotes: 11

OSGI Java
OSGI Java

Reputation: 645

rootPath determines the root of where the static content resides:

@Module({
    imports: [
        ServeStaticModule.forRoot({
            rootPath: join(__dirname, '..', 'static'),
        }),
    ],
})

In this case the root is the static folder.

You can have for example the following structure:

static
    page
      png
        sky.png
      index.html

In order to get sky.png the request would be: http://localhost:3000/page/png/sky.png

In order to get index.html the request would be: http://localhost:3001/page/index.html

In addition, here is documentation for ServeStaticModuleOptions: https://github.com/nestjs/serve-static/blob/master/lib/interfaces/serve-static-options.interface.ts

I would point your attention to renderPath. I do not find the documentation for renderPath very clear but here in this bug report the expected behavior is that the renderPath re-maps the local path to URL as you kind of wanted: https://github.com/nestjs/serve-static/issues/26

You can also take a look at Express/Fastify to see how this option is handled there as these are the servers used under the cover.

Upvotes: 1

Jay McDoniel
Jay McDoniel

Reputation: 70510

You can set the renderPath options on the ServeStaticModule. Normally the path is '*' so anything that doesn't match your other defined routes attempts to get sent via res.sendFile(). If you want to make it some/path/to/file you can set renderPath: 'some/path/to/files/* and it should work out.

Upvotes: 1

Related Questions