Nest: Nest 6 and app.disable('x-powered-by')

Created on 20 Mar 2019  路  5Comments  路  Source: nestjs/nest

I'm submitting a...


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

Current behavior

With Nest 6, what is the proper way to disable the x-powered-by: Express header? It seems like disable(...) has been removed.

I don't see any relevant information in the v6 documentation about this particular change.

What is the motivation / use case for changing the behavior?

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.disable('x-powered-by'); // no longer works
  app.use(nocache());
  await app.listen(1338);
}
bootstrap();

Environment


Nest version: 6.0.1

Most helpful comment

Thanks. I think the migration guide could use this information as well.

All 5 comments

const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.disable('x-powered-by');

NestExpressApplication is imported from @nestjs/platform-express package.
see https://docs.nestjs.com/first-steps#platform

Thanks. I think the migration guide could use this information as well.

@kamilmysliwiec interestingly when I tried your suggestion with nest 6.2.4, I am unable to properly remove x-powered-by: Express header. when I add app.disable('x-powered-by'); and check the headers again, the header is changed to capitalised word version X-Powered-By.

I have tried adding both casings as:
app.disable('x-powered-by');
app.disable('X-Powered-By');

but api is still responding with X-Powered-By: Express.

The only thing that worked is overwriting it with custom string.

app.use(function(req, res, next) {
    res.header('x-powered-by', 'Blood, sweat, and tears.');
    next();
  });

@sajidali you can also create an express instance and configure it as you want, and then pass it back to the NestFactory 馃槂

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings