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;