Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable X-Powered-By in nestjs does not work

Tags:

nestjs

I want to disable X-Powered-By in nestjs like the following, but it does not work.

main.ts:

async function bootstrap() {
    const logger = new Logger('bootstrap') 
    const app = await NestFactory.create<NestExpressApplication>(AppModule);
  
    app.disable('X-Powered-By') // this line
    ...
    
    const PORT = process.env.PORT
    await app.listen(PORT);
    logger.log(`Application is start on port : ${PORT}`)
  }
  
  bootstrap();

After disabling the X-Powered-By header, in the next requests, that X-Powered-By header still exists.

Where am I doing something wrong?

like image 978
Ghasem Khalili Avatar asked Dec 21 '25 00:12

Ghasem Khalili


2 Answers

If app.disable('x-powered-by') does not work, you can try/fix it with:

app.getHttpAdapter().getInstance().disable('x-powered-by');
like image 94
Christian van R Avatar answered Dec 24 '25 10:12

Christian van R


If you are using TypeScript and you get an error in your IDE saying Property 'disable' does not exist on type 'INestApplication', then specify the app type in your NestFactory.create call.

Your code should look like below, and the error on the app.disable call will be gone.

const app = await NestFactory.create<NestExpressApplication>(AppModule)

app.disable('x-powered-by')
like image 38
Enve Avatar answered Dec 24 '25 10:12

Enve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!