Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS headers for static files

We add security headers to NestJS requests.

We also serve static html files.

app.useGlobalInterceptors(new TransformHeadersInterceptor());

app.useStaticAssets(join(__dirname, "..", "public"), {
    //index: false,
    redirect: false
});

The headers are added to NestJS controller output but not to html files from public.

How would we do this? Do we need to go to Express level?

like image 687
KingOfCoders Avatar asked Aug 31 '25 10:08

KingOfCoders


1 Answers

You have to set the headers to the static folder, this is the Express docs

one example could be this in the main.ts

 app.useStaticAssets(`${__dirname}/public`,
  { setHeaders: (res, path, stat) => {
    res.set('Access-Control-Allow-Origin', '*');
  }});

you can add you headers in the setHeaders function Hope this help someone

like image 139
Alexander Rojas Avatar answered Sep 05 '25 06:09

Alexander Rojas