Commit dcd5e09b authored by Balasuriya D.A.M.'s avatar Balasuriya D.A.M.

API Error Handlers

parent 11ac71eb
const notFound = (req, res, next) => {
//connect with server.js
const error = new Error(`Not Found - ${req.originalUrl}`);
res.status(404);
next(error);
};
const errorHandler = (err, req, res, next) => {
const statusCode = res.statusCode === 200 ? 500 : res.statusCode;
res.status(statusCode);
res.json({
message: err.message,
stack: process.env.NODE_ENV === "production" ? null : err.stack,
});
};
module.exports = { notFound, errorHandler };
\ No newline at end of file
......@@ -5,6 +5,7 @@ const { chats } = require("./data/data");
const connectDB = require("./config/db");
const colors = require("colors");
const userRoutes = require("./routes/userRoutes");
const { notFound,errorHandler} = require("./middleware/errorMiddleware");
dotenv.config();
......@@ -21,6 +22,10 @@ app.get("/", (req, res) => {
app.use("/api/user", userRoutes);
//API Error Handling
app.use(notFound);
app.use(errorHandler);
const PORT = process.env.PORT || 5000;
app.listen(5000, console.log(`Server Started on PORT ${PORT}`.yellow.bold));
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment