webmasternewbie
webmasternewbie

Reputation: 167

node.js - how to modiffy response headers

Platform: node.js + express and request modules (beginner).

HI, I got simply router.get followed by a request and I want to modify the headers it get from external server, and send it to the user that requested the specific data.

router.get('/', function(req, res){
    var someUserRequest = request('http://google.com', function(error, resp, body){
        if (error){
        console.log(error);
        }
        console.log(resp.headers); //lets assume that there is 'x-content-type-options' that I don't want for example
    }
    req.pipe(someUserRequest).on('response', function(response){
    delete response.headers['x-content-type-options'];
    console.log(response.headers); // everything looks fine here - no 'x-content-type-options'
    }).pipe(res);
});
module.exports = router;

So it seams that there is no 'x-content-type-options'. However when I check this out in chrome developer tools (network) I can clearly see 'x-content-type-options: nosniff' - even when I clear all history/cache.

So why it's not working? Can somebody explain me where is the error?

Upvotes: 0

Views: 958

Answers (1)

Michael Curry
Michael Curry

Reputation: 989

I think that you might need to splice the array instead, try putting a test condition before and after you

delete

to see if

['x-content-type-options'] 

exists in response.headers.

If it does then you can probably splice instead.

Read more on delete here

Upvotes: 1

Related Questions