Home > other >  Problem with multiple call of res : Cannot set headers after they are sent to the client
Problem with multiple call of res : Cannot set headers after they are sent to the client

Time:11-23

Hy, I'm facing the same problem as I tried to implement 2 res's : I need them both but I receive the error message : "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client". So How can I make only one res that wraps the 2 statments ? I know I can put the status in the redirect function as first parameter but how about the json message ?

module.exports = (app) => {
app.get("/api/logout", (req, res) => {
    res.cookie('jwtCookie', '', {
        maxAge: 1
    });
    res.status(200).json({ message: "user disconnected" });
    res.redirect('/');
});

};

CodePudding user response:

You can only send one response per incoming http request. And, in your code, both of these each attempt to send a response:

res.status(200).json({ message: "user disconnected" });
res.redirect('/');

The first line sends a JONS response with a 200 status. The second line res.redirect() sends a 302 response and sets the Location header.

Because Express can see that you already sent one response with the first line, it gives you an error message when you try to send a second response with that second line of code.

So, you have to decide if you're sending a JSON response OR sending a redirect. Pick one or the other. You cannot send two separate responses.

Since you're first sending JSON, it appears that the client at the other end is some sort of code (like perhaps an Ajax call in a web page). If that's the case, you can include a redirect location in the JSON response and then rely on the client to redirect itself.

As it turns out, if the client is a programmable client such as an Ajax call in web page Javascript, the redirect doesn't cause the browser to change the web page anyway. You have to have the client-side Javascript implement that if that's what you want to happen.

So, you could do this:

module.exports = (app) => {
    app.get("/api/logout", (req, res) => {
        res.cookie('jwtCookie', '', {
            maxAge: 1
        });
        res.status(200).json({ 
            message: "user disconnected",
            redirect: "/" 
        });
    });
};

Then, your Javascript client at the other end would look for the redirect property on the response object and, if present, it would set window.location to the new value to cause the browser to change what page it was viewing.

Or, if you want the client to actually look for a 302 response and to get the Location header from that response, then you can do this:

module.exports = (app) => {
    app.get("/api/logout", (req, res) => {
        res.cookie('jwtCookie', '', {
            maxAge: 1
        });
        res.location('/');
        res.status(302).json({ 
            message: "user disconnected"
        });
    });
};

CodePudding user response:

You must either redirect your user's browser or send a JSON message. You cannot do both for one single request.

  • Related