Express Production Setup - 5 | Handle Invaild Json
const express = require("express");
const app = express();
app.use(express.json())
// Error handling middleware
app.use((err, req, res, next) => {
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
res.status(400).json({ error: "Invalid JSON" });
} else {
next(); // Passes the error to the next error handler if it's not related to JSON parsing
}
});
app.get("/", (req, res) => {
res.json(req.body);
});
app.listen(3000, () => console.log("server running on port 3000"));
Comments
Post a Comment