Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. Why does it occurs?

I am new to this topic of Rest Apis, and I was creating a simple one where I receive a json with movie data and give a small response. As I am new I want to start with what would be the logic and it is an array that is saving data.

But when I send data through PostMan, it saves everything well and does its job, but I get this error on the console.

someone who can guide me please

MY CODE

router.post('/', (req, res) => {
    const {titulo, director, year, rating} = req.body;
    if (titulo && director && year && rating) {
        const id = movies.length + 1;
        const newMovie = {...req.body, id};
        movies.push(newMovie);
        res.json(movies);
    } else {
        res.status(500).json({error: 'Ha Ocurrido un error'});
    }
    res.send('recibido');
});

module.exports = router;

CMD ERROR

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.setHeader (node:_http_outgoing:576:11)
    at ServerResponse.header (C:\Users\ManuelLeigh\Desktop\restapi\node_modules\express\lib\response.js:776:10)
    at ServerResponse.send (C:\Users\ManuelLeigh\Desktop\restapi\node_modules\express\lib\response.js:170:12)
    at C:\Users\ManuelLeigh\Desktop\restapi\src\routes\movies.js:20:9
    at Layer.handle [as handle_request] (C:\Users\ManuelLeigh\Desktop\restapi\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\ManuelLeigh\Desktop\restapi\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\ManuelLeigh\Desktop\restapi\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\ManuelLeigh\Desktop\restapi\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\ManuelLeigh\Desktop\restapi\node_modules\express\lib\router\index.js:281:22

Postman Header:

Content-Type: application/json
like image 439
Manuel Leigh Avatar asked Sep 17 '25 19:09

Manuel Leigh


2 Answers

Your code is trying to send response after it is sent. Check for following lines, in if and else,, you are already sending response to server and after this is processed, on last line, you are sending some text.

   if (titulo && director && year && rating) {
        res.json(movies);
    } else {
        res.status(500).json({error: 'Ha Ocurrido un error'});
    }
    res.send('recibido');

Update your code as below,

router.post('/', (req, res) => {
    const {titulo, director, year, rating} = req.body;
    if (titulo && director && year && rating) {
        const id = movies.length + 1;
        const newMovie = {...req.body, id};
        movies.push(newMovie);
        res.json(movies);
    } else {
        res.status(500).json({error: 'Ha Ocurrido un error'});
    }
   // res.send('recibido');
});

module.exports = router;
like image 142
Vikas Keskar Avatar answered Sep 20 '25 11:09

Vikas Keskar


after if-else block, you again try to send response.By putting return after sending response in each block, you can fix your problem or other simple way, just comment res.send("recibido"). simple way is to just comment res.send("recibido")

you can write your code this way:

router.post('/', (req, res) => {
const {titulo, director, year, rating} = req.body;
if (titulo && director && year && rating) {
    const id = movies.length + 1;
    const newMovie = {...req.body, id};
    movies.push(newMovie);
    res.json(movies);      
} else {
    res.status(500).json({error: 'Ha Ocurrido un error'});        
}
   // res.send('recibido');
});

module.exports = router;
like image 29
rmnkh Avatar answered Sep 20 '25 09:09

rmnkh