[0] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0] at new NodeError (node:internal/errors:400:5)
[0] at ServerResponse.setHeader (node:_http_outgoing:663:11)
[0] at ServerResponse.header (C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\response.js:794:10)
[0] at ServerResponse.json (C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\response.js:275:10)
[0] at errorHandler (file:///C:/Users/saadaoui/Desktop/MERN%20E-commerce/backend/middlewares/errorMiddleware.js:14:26)
[0] at Layer.handle_error (C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\router\layer.js:71:5)
[0] at trim_prefix (C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\router\index.js:326:13)
[0] at C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\router\index.js:286:9
[0] at Function.process_params (C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\router\index.js:346:12)
[0] at next (C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\router\index.js:280:10)
أعتقد أن مكان الخطأ في errorrMiddleware.js
errorrMiddleware.js
const notFound = (req, res, next) => {
const error = new Error(`Not Found - ${req.originalUrl}`);
res.status(404);
next(error);
};
const errorHandler = (err, req, res, next) => {
let statusCode = res.statusCode === 200 ? 500 : res.statusCode;
let message = err.message;
// NOTE: checking for invalid ObjectId moved to it's own middleware
// See README for further info.
res.status(statusCode).json({
message: message,
stack: process.env.NODE_ENV === "production" ? null : err.stack,
});
};
export { notFound, errorHandler };
server.js
import express from "express";
import dotenv from "dotenv";
import productRoutes from './routes/productRoutes.js'
dotenv.config();
import connectDB from "./config/db.js"
import { errorHandler, notFound } from "./middlewares/errorMiddleware.js";
const port = process.env.PORT || 5000;
connectDB()
const app = express()
app.use(express.json());
app.use('/api/products', productRoutes)
app.get('/', (req, res) => {
res.send('API is running ...')
})
app.use(notFound)
app.use(errorHandler)
app.listen(port, () => console.log(`Server is running on port ${port}`))
السؤال
محمود سعداوي2
السلام عليكم.
أواجه الخطأ التالي:
[0] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0] at new NodeError (node:internal/errors:400:5)
[0] at ServerResponse.setHeader (node:_http_outgoing:663:11)
[0] at ServerResponse.header (C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\response.js:794:10)
[0] at ServerResponse.json (C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\response.js:275:10)
[0] at errorHandler (file:///C:/Users/saadaoui/Desktop/MERN%20E-commerce/backend/middlewares/errorMiddleware.js:14:26)
[0] at Layer.handle_error (C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\router\layer.js:71:5)
[0] at trim_prefix (C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\router\index.js:326:13)
[0] at C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\router\index.js:286:9
[0] at Function.process_params (C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\router\index.js:346:12)
[0] at next (C:\Users\saadaoui\Desktop\MERN E-commerce\node_modules\express\lib\router\index.js:280:10)
أعتقد أن مكان الخطأ في errorrMiddleware.js
errorrMiddleware.js
const notFound = (req, res, next) => { const error = new Error(`Not Found - ${req.originalUrl}`); res.status(404); next(error); }; const errorHandler = (err, req, res, next) => { let statusCode = res.statusCode === 200 ? 500 : res.statusCode; let message = err.message; // NOTE: checking for invalid ObjectId moved to it's own middleware // See README for further info. res.status(statusCode).json({ message: message, stack: process.env.NODE_ENV === "production" ? null : err.stack, }); }; export { notFound, errorHandler };
server.js
import express from "express"; import dotenv from "dotenv"; import productRoutes from './routes/productRoutes.js' dotenv.config(); import connectDB from "./config/db.js" import { errorHandler, notFound } from "./middlewares/errorMiddleware.js"; const port = process.env.PORT || 5000; connectDB() const app = express() app.use(express.json()); app.use('/api/products', productRoutes) app.get('/', (req, res) => { res.send('API is running ...') }) app.use(notFound) app.use(errorHandler) app.listen(port, () => console.log(`Server is running on port ${port}`))
شكرا للمساعدة
5 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.