1

I have to remove X-powered-by Express header, I found theses solution

app.disable('x-powered-by');

or

app.use(function (req, res, next) {
  res.removeHeader("X-Powered-By");
  next();
});

but in this project we don't use express in a basic way, we import Express this way in multiple files

import {Express} from 'express'; // @types/Express

and then we call Express.multer.file

I'm new on this project and also on backend development, so how can I remove this header using this way and not the basic way ?

zedArt
  • 487
  • 1
  • 10
  • 27

4 Answers4

3

You can still use it the "basic" way:

import express, { Express } from 'express';

const app: Express = express();

app.disable('x-powered-by');
robertklep
  • 198,204
  • 35
  • 394
  • 381
0

You can remove header in sequence.ts file like this.

export class MySequence extends MiddlewareSequence {
   async handle(context: RequestContext) {
      const res = context.response;
      res.removeHeader('X-Powered-By');

      await super.handle(context);
   }
}

or use helmet package for security of headers. thus remove headers telling how software is developed

akadirdev
  • 70
  • 7
  • I have a sequence.ts file where there is only this : export class MySequence extends MiddlewareSequence {} I add your recommendation code but how can I test it, if I run website the header is still there – zedArt May 25 '22 at 10:48
  • When i test any endpoint in postman, i can't see this header – akadirdev May 25 '22 at 11:02
0

so, the solution I found was in loopback documentation - Express settings

I just add this to my rest config object :

  const config = {
    rest: {
      expressSettings: {
        'x-powered-by': false,
      },
      ...
      ...
    }
   }
zedArt
  • 487
  • 1
  • 10
  • 27
0

In case anyone looking for the way to disable it for tinyhttp:

import { App } from '@tinyhttp/app';

const app = new App({
  settings: {
    xPoweredBy: false
  }
});
Mechanic
  • 5,015
  • 4
  • 15
  • 38