-1

Server running on port 5000.

node:internal/errors:484
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:393:5) at ServerResponse.setHeader (node:_http_outgoing:644:11) at ServerResponse.header (C:\Users\ASUS\Desktop\WebDev\React\Netflix\netflix\Backend\node_modules\express\lib\response.js:794:10) at ServerResponse.send (C:\Users\ASUS\Desktop\WebDev\React\Netflix\netflix\Backend\node_modules\express\lib\response.js:174:12) at ServerResponse.json (C:\Users\ASUS\Desktop\WebDev\React\Netflix\netflix\Backend\node_modules\express\lib\response.js:278:15) at C:\Users\ASUS\Desktop\WebDev\React\Netflix\netflix\Backend\Routes\movies.js:32:25 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: 'ERR_HTTP_HEADERS_SENT' }

{ npm start err: Proxy error: Could not proxy request /movies/random?type=undefined from localhost:3000 to http://localhost:5000/api/. See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

Proxy error: Could not proxy request /lists from localhost:3000 to http://localhost:5000/api/. See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

Proxy error: Could not proxy request /movies/random?type=undefined from localhost:3000 to http://localhost:5000/api/. See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

Proxy error: Could not proxy request /lists from localhost:3000 to http://localhost:5000/api/. See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

Proxy error: Could not proxy request /movies/random?type=undefined from localhost:3000 to http://localhost:5000/api/. See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

Proxy error: Could not proxy request /lists from localhost:3000 to http://localhost:5000/api/. See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

Proxy error: Could not proxy request /movies/random?type=undefined from localhost:3000 to http://localhost:5000/api/. See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

Proxy error: Could not proxy request /lists from localhost:3000 to http://localhost:5000/api/. See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

package.json :- "proxy": "http://localhost:5000/api/", }

Node.js v18.12.1

I am opening this project after completing it few days back, But now it is not working and throwing errors in Backend and Frontend as well. Please help me to fix this. Plus proxy errors at Frontend. Errors showed up res.stastus(200).json(err)

Movies.js:

const express = require('express');
const router = express.Router();
const Movie = require("../Models/Movie");
const verify = require('../verifyToken');

//GET Random Movie or Series for featured section
router.get("/random", verify, async (req,res) => {
    const type = req.query.type;
    let movie;
    try{
        if(type === "series"){
            movie = await Movie.aggregate([
                {
                    $match: {isSeries: true}
                },
                {
                    $sample : {size: 1}
                },
            ]);
        }else{
            movie = await Movie.aggregate([
                {
                    $match: {isSeries: false}
                },
                {
                    $sample: {size: 1}
                }
            ]);
        }
        res.status(200).json(movie);
    }catch(err){
        res.status(500).json(err);
    }    
});

router.post("/", verify, async (req,res) => {
    if(req.user.isAdmin){
        const newMovie = new Movie(req.body);

        try{
            const savedMovie = await newMovie.save();
            res.status(200).json(savedMovie);
        }catch(err){
            res.status(500).json(err);
        }
    }
    else{
        res.status(403).json("Unauthorized access.");
    }  
});

//UPDATE 
router.put("/:id", verify, async (req,res) => {
    if(req.user.isAdmin){
        try{
            const updatedMovie = await Movie.findByIdAndUpdate(req.params.id,{
                $set: req.body    
            },{new: true});
            res.status(200).json(updatedMovie);

        }catch(err){
            res.status(500).json(err);
        }
        
    }else{
        res.status(403).json("Unauthorized access.");
    }
})

//DELETE
router.delete("/:id", verify, async (req,res) => {
    if(req.user.isAdmin){
        try{
            const deletedMovie = await Movie.findByIdAndDelete(req.params.id);
            res.status(200).json("Movie has been deleted.");
        }catch(err){
            res.status(500).json(err);
        }
    }else{
        res.status(403).json("Unauthorized access.");
    }
});

//GET
router.get("/:id", verify, async (req,res) => {
    try{
        const getMovie = await Movie.findById(req.params.id);
        res.status(200).json(getMovie);
    }catch(err){
        res.status(500).json(err);
    }
});

//GET ALL MOVIES
router.get("/", verify, async (req,res) => {
    if(req.user.isAdmin){
        try{
            const allMovies = await Movie.find();
            res.status(200).json(allMovies.reverse());
        }catch(err){
            res.status(500).json(err);
        }
    }else{
        res.status(403).json("Unauthorized access.");
    }     
});

module.exports = router;
  • 1
    Mostly this error will comes when you attempt to send multiple responses to an HTTP request. Could you provide more specific information about the request for which this error is being triggered? – sms Aug 08 '23 at 14:11
  • Line 32 is around “/stats” endpoint? – pierpy Aug 08 '23 at 14:12
  • ok i will edit it – Abhishek gaikwad Aug 08 '23 at 14:15
  • Code doesn't generally just stop working. Have you changed the code in any way? Have you updated the Node version, or any of the libraries/tools? What's your development build process / how are you proxying the endpoints? – Andy Aug 08 '23 at 14:15
  • how to update nodeversion – Abhishek gaikwad Aug 08 '23 at 14:20
  • @Abhishekgaikwad it was a question not a suggestion :) Have you, over the few days since you worked on the code, made any updates to the environment, the tools/libraries you're using, or your dev build process? – Andy Aug 08 '23 at 14:25
  • No not all. But can I reinstall all dependencies – Abhishek gaikwad Aug 08 '23 at 14:29
  • Check https://stackoverflow.com/questions/45367298/could-not-proxy-request-pusher-auth-from-localhost3000-to-http-localhost500 – James Aug 08 '23 at 14:30

0 Answers0