在 Nestjs 中禁用 X-Powered-By 不起作用

AR *_*ond 12 nestjs

我想像下面这样在nestjs中禁用X-Powered-By,但它不起作用。

主要.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();
Run Code Online (Sandbox Code Playgroud)

禁用X-Powered-By标头后,在下一个请求中,该X-Powered-By标头仍然存在。

我哪里做错了什么?

Chr*_*n R 23

如果app.disable('x-powered-by')不起作用,您可以尝试/修复它:

app.getHttpAdapter().getInstance().disable('x-powered-by');
Run Code Online (Sandbox Code Playgroud)


Env*_*nve 12

如果您使用 TypeScript 并且在 IDE 中收到错误消息Property 'disable' does not exist on type 'INestApplication',请在调用中指定应用程序类型NestFactory.create

您的代码应如下所示,调用时的错误app.disable将会消失。

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

app.disable('x-powered-by')
Run Code Online (Sandbox Code Playgroud)


Sch*_*utt 4

尝试使用: app.disable('x-powered-by')- 所以全部小写,它应该可以工作!