Stepan Rafael
Stepan Rafael

Reputation: 415

ExpressJS response middleware

I've made an HttpInterceptor for the front-end that send every request with some default headers and automatically encrypt body/url for every request and a middleware for the back-end that check the headers and decrypt the packet if needed.. Now I have a problem with the response middleware, because I want to send the response with encrypted body only for some requests.

app.use((req,res,next)=>{

    if(req.headers['x-data-encoded'] && (req.headers['x-server'] == "HP")){

        res.append('X-Encoded-Data', true);

        var nsp = res.send;

        res.send = function(data){
            var body = Crypto.encodeData(data); // Result a string of letters and numbers
            nsp.apply(this, body);
        }

    }

    next();
});

Caught exception: TypeError: CreateListFromArrayLike called on non-object

Upvotes: 2

Views: 351

Answers (1)

David Vicente
David Vicente

Reputation: 3111

I think that error appears because send method waits an object and it is receiving a String. If you assign manually body an object this error should dissapear or change to another one.

Besides, second parameter of apply should be an array.

Hope it helps

Upvotes: 1

Related Questions