0

I am trying to remove x-powered-by header from response headers in angular cli project.As there is no direct access for express, so how can we remove header from all request's response header. I tried following -

const PROXY_CONFIG = [
 {
   context: [
     '/'
   ],
   secure: false,
   bypass: function (req, res) {
     res.removeHeader('x-powered-by');
   }
 }
];

module.exports = PROXY_CONFIG;

But it worked only for first request, for all other request it didn't work.

I tried following as well but the function doesn't get triggered -

 const PROXY_CONFIG = [
 {
   context: [
     '/'
   ],
   secure: false,
   onProxyRes: function (proxyRes, req, res) {
     delete proxyRes.headers['x-powered-by']; 
   }
 }
];

module.exports = PROXY_CONFIG;

I referred following links -

Angular: add custom HTTP response headers to dev `ng serve`

How set proxy headers in proxy.config.json file for angularcli project

2
  • i am facing similar issue , did you find a solution for this, i am trying it using interceptors, This is still not working......(event instanceof HttpResponse) { event = event.clone({ headers: event.headers.delete('x-aspnet-version') .delete('x-powered-by') }); return event; } Commented May 7, 2020 at 15:41
  • I think you can do that easily using hemlet.js on express side. Commented May 12, 2020 at 11:11

2 Answers 2

0

You can remove existing header in Interceptors.

 if (!req.headers.has('X-Powered-By')) {
 req = req.clone({ headers: req.headers.delete('X-Powered-By','xxxxx') });

To Check the current value of the header.

req.headers.get('Accept')
Sign up to request clarification or add additional context in comments.

5 Comments

It is a response header, i think we would need to handle it when sending response right.
yes but if you didnt had accept to the response header so when fetching it with angular, interceptions can be used for the same task.
i am trying to do same , but it is not working event = event.clone({ headers: event.headers.delete('x-aspnet-version').delete('x-powered-by') });
You are doing it wrong req = req.clone({ headers: req.headers.delete(''x-powered-by','x-aspnet-version') });
i had tried both dint work, finally removed it using server settings to remove headers.
0

It does work for the proxies correctly. So for all the request it was resolved by adding it on express side. Used helmetjs.

const helmet = require('helmet')

app.use(helmet())
app.disable('x-powered-by')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.