nestjs / serve-static

Serve static websites (SPA's) using Nest framework (node.js) šŸ„¦

Home Page:https://nestjs.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

renderPath option doesn't work

kuroyuki opened this issue Ā· comments

I'm submitting a...


[ ] Regression 
[x ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

If StaticServer imported with "renderPath" option it doesn't serve assets on /static

Expected behavior

StaticServer should provide all the assets on /static

Minimal reproduction of the problem with instructions

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';

@Module({
  imports: [
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, 'public'),
      renderPath: "/static" 
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Suggestions for fixing it

I would suggest to edit express.loader.ts file according to Express mount path specification.
Replace

    const clientPath = options.rootPath;
    const indexFilePath = this.getIndexFilePath(clientPath);

    app.use(express.static(clientPath, options.serveStaticOptions));
    app.get(options.renderPath, (req: any, res: any) =>
      res.sendFile(indexFilePath)
    );

with

    const clientPath = options.rootPath;
    app.use(options.renderPath, express.static(clientPath, options.serveStaticOptions));

Is this also the reason I am not able to serve two directories, one / and the other /admin?

Yes, seems the issue should be the reason.

Based on conversation here, is this issue still unresolved, despite the PR?

We have added the new property serveRoot in the 2.0.0 release. Use this to serve all the assets under the /static path. Example:

ServeStaticModule.forRoot({
   rootPath: join(__dirname, 'public'),
   serveRoot: "/static" 
}),